打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
展开label,利用YYText实现文字显示不完末尾添加全文

效果图:

操作

先github下载<YYText>文件导入,

代码如下:

#import "ViewController.h"#import "YYLabel.h"#import "NSAttributedString+YYText.h"#import <UIKit/UIKit.h>@interface ViewController ()    @property (nonatomic,strong) YYLabel *label;;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        NSMutableAttributedString *text = [NSMutableAttributedString new];    UIFont *font = [UIFont systemFontOfSize:16];        // 添加文本    NSString *title = @"dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前的群无多群无多群无多群无多群无多群多群无多群无多群无多群无多群多群多群多群当前的群无多群多群无多群多群多群多群多群多群多群多群的权威的权威的期望多无群多群无多群多群多群多群无多群无多群无多群无多群无多群无多群多群无多群无多群多群无多群多群无多无多无群多多群无多群多群多群多群无多群多无!";        [text appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];        text.yy_font = font ;    _label = [YYLabel new];    _label.userInteractionEnabled = YES;    _label.numberOfLines = 0;    _label.textVerticalAlignment = YYTextVerticalAlignmentTop;    _label.frame = CGRectMake(40,60, self.view.frame.size.width-80,150);    _label.attributedText = text;    [self.view addSubview:_label];        _label.layer.borderWidth = 0.5;    _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;        // 添加全文    [self addSeeMoreButton];}#pragma mark - 添加全文- (void)addSeeMoreButton {        __weak __typeof(self) weakSelf = self;        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...全文"];    YYTextHighlight *hi = [YYTextHighlight new];    [hi setColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];        hi.tapAction = ^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect) {
// 点击全文回调 YYLabel *label = weakSelf.label; [label sizeToFit]; }; [text yy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"全文"]]; [text yy_setTextHighlight:hi range:[text.string rangeOfString:@"全文"]]; text.yy_font = _label.font; YYLabel *seeMore = [YYLabel new]; seeMore.attributedText = text; [seeMore sizeToFit]; NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter]; _label.truncationToken = truncationToken;}

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
YYText Powerful text framework for iOS to display and edit rich text.
iOS YYText的使用笔记二(YYLabel聊天表情 文字并排)
反斜杠与转义
时间差的计算
Unicode Patch for Flex-2.5.4
词法分析器生成工具flex
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服