用YYKit实现点击更多显示完整string

2018-01-19  本文已影响611人  MichalWilson
点击后

#define screenwidth [UIScreen mainScreen].bounds.size.width

#define screenheight [UIScreen mainScreen].bounds.size.height

@interface TestViewController ()

@property (nonatomic,strong)YYLabel * label;

@property (nonatomic,strong)YYLabel * seeMore;

@property (nonatomic, assign, getter = isVisiable) BOOL visiable;

@property(nonatomic,assign,getter= isSelf_adaption)BOOLself_adaption;

@property (nonatomic, strong) NSIndexPath* indexPath_cell;

@end

@implementationTestViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    NSString * discribeText = @"前端领域生态繁荣,致力于提高生产效率的技术、工具不断推陈出新,可谓群雄割据且各有千秋。企业及前端开发者在实际开发中可选择的空间大大增加但也面临着选择困难症。抑或虽已确定了技术栈,但实际开发中却时不时遭遇各种“坑”的牵绊。深入了解各大技术的优势与劣势,同时结合自身业务需求,你的技术选择之路将会变得更加明朗;深入了解其他企业对各大技术的应用实践及所踩过的坑,你的技术应用之路将会更加顺畅。毕竟有些人,有些企业趟过的坑,我们不必再趟。 基于这个目的,我们特别策划了SDCC 邀请来自阿里巴巴、苏宁云商、美团点评、饿了么、去哪儿网、白鹭时代等多家企业的前端专家及技术图书作者,围绕React、AngularJS、Weex前端热门框架在企业中的应用实践,及WebAssembly、MVVM等技术热点展开深入分享,帮助大家解决实际生产中遇到问题。而且,每个演讲时段均设有答疑交流环节,与会者可以提前将平时遇到的实际难题记录下来,到时可以和讲师零距离交流";

    NSMutableAttributedString * text = [NSMutableAttributedString new];

    UIFont* font = [UIFontsystemFontOfSize:16];

    [textappendAttributedString:[[NSAttributedString alloc]initWithString:discribeText]];

    text.yy_font= font;

    _label= [YYLabelnew];

    _label.userInteractionEnabled = YES;

    _label.numberOfLines = 0;

    _label.textVerticalAlignment = YYTextVerticalAlignmentCenter;

    _label.frame=CGRectMake(10,100,screenwidth-20,60);

    _label.attributedText = text;

    [self.view addSubview:_label];

    _label.layer.borderWidth = 1.0;

    _label.layer.borderColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0].CGColor;

    [self addSeeMoreButton];

}

-(void)addSeeMoreButton{

    __weaktypeof(self) weakSelf =self;

    NSMutableAttributedString * text = [[NSMutableAttributedString alloc]initWithString:@"......more"];

    YYTextHighlight * high = [YYTextHighlight new];

    [highsetColor:[UIColor colorWithRed:0.6 green:.08 blue:1.0 alpha:1.0]];

    high.tapAction= ^(UIView*_NonnullcontainerView,NSAttributedString*_Nonnulltext,NSRangerange,CGRectrect) {

        YYLabel* label = weakSelf.label;

        [labelsizeToFit];

        weakSelf.label.userInteractionEnabled = YES;

        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(packUp:)];

        [weakSelf.labeladdGestureRecognizer:tap];

    };

    [textyy_setColor:[UIColor colorWithRed:0 green:0.45 blue:1.00 alpha:1.00] range:[text.string rangeOfString:@"more"]];

    [textyy_setTextHighlight:high range:[text.string rangeOfString:@"more"]];

    text.yy_font=_label.font;

    _seeMore= [YYLabelnew];

    _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;

}

-(void)packUp:(UITapGestureRecognizer *)tap{

    _label.text = @"前端领域生态繁荣,致力于提高生产效率的技术、工具不断推陈出新,可谓群雄割据且各有千秋。企业及前端开发者在实际开发中可选择的空间大大增加但也面临着选择困难症。抑或虽已确定了技术栈,但实际开发中却时不时遭遇各种“坑”的牵绊。深入了解各大技术的优势与劣势,同时结合自身业务需求,你的技术选择之路将会变得更加明朗;深入了解其他企业对各大技术的应用实践及所踩过的坑,你的技术应用之路将会更加顺畅。毕竟有些人,有些企业趟过的坑,我们不必再趟。 基于这个目的,我们特别策划了SDCC 邀请来自阿里巴巴、苏宁云商、美团点评、饿了么、去哪儿网、白鹭时代等多家企业的前端专家及技术图书作者,围绕React、AngularJS、Weex前端热门框架在企业中的应用实践,及WebAssembly、MVVM等技术热点展开深入分享,帮助大家解决实际生产中遇到问题。而且,每个演讲时段均设有答疑交流环节,与会者可以提前将平时遇到的实际难题记录下来,到时可以和讲师零距离交流";

    _label.frame=CGRectMake(10,100,screenwidth-20,60);

    [_label removeGestureRecognizer:tap];

}

上一篇下一篇

猜你喜欢

热点阅读