超过一定行数的label强制在末尾加上一个...展开且可以点击成全文

问题来源  ios 超过一定行数的label强制在末尾加上一个...展开且可以点击成全文

https://segmentfault.com/q/1010000004452279  

 

利用YYText可以简单的解决这个问题。  


关键字: truncationToken /  YYText 

 





- (void)viewDidLoad {

    [superviewDidLoad];

    

    //需求类似:https://segmentfault.com/q/1010000004452279

    

    //代码来自:YYText示例demo

    

    NSMutableAttributedString *text = [NSMutableAttributedStringnew];

    UIFont *font = [UIFontsystemFontOfSize:16];

    

    

    //添加文本

    NSString *title =@"豫章故郡,洪都新府。星分翼轸,地接衡庐。襟三江而带五湖,控蛮荆而引瓯越。物华天宝,龙光射牛斗之墟;人杰地灵,徐孺下陈蕃之榻。雄州雾列,俊采星驰。台隍枕夷夏之交,宾主尽东南之美。都督阎公之雅望,棨戟遥临;宇文新州之懿范,襜帷暂驻。十旬休假,胜友如云;千里逢迎,高朋满座。腾蛟起凤,孟学士之词宗;紫电青霜,王将军之武库。家君作宰,路出名区;童子何知,躬逢胜饯。时维九月,序属三秋。潦水尽而寒潭清,烟光凝而暮山紫。俨骖騑于上路,访风景于崇阿。临帝子之长洲,得仙人之旧馆。层台耸翠,上出重霄;飞阁流丹,下临无地。鹤汀凫渚,穷岛屿之萦回;桂殿兰宫,列冈峦之体势。";

    [text appendAttributedString:[[NSAttributedStringalloc] initWithString:titleattributes:nil]];

   

    text.yy_font = font ;


    

    _label = [YYLabelnew];

    _label.userInteractionEnabled =YES;

    _label.numberOfLines =0;

    _label.textVerticalAlignment =YYTextVerticalAlignmentTop;

    _label.frameCGRectMake(60,60, 260,260);

    _label.attributedText = text;


    [self.viewaddSubview:_label];

    

    _label.layer.borderWidth =0.5;

    _label.layer.borderColor = [UIColorcolorWithRed:0.000green:0.463blue:1.000alpha:1.000].CGColor;

    

    

    [selfaddSeeMoreButton];

    

}




- (void)addSeeMoreButton {

    __weaktypeof(self) _self =self;

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

    

    YYTextHighlight *hi = [YYTextHighlightnew];

    [hi setColor:[UIColorcolorWithRed:0.578green:0.790blue:1.000alpha:1.000]];

    hi.tapAction = ^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect) {

        YYLabel *label = _self.label;

        [label sizeToFit];

    };

    

    [text yy_setColor:[UIColorcolorWithRed:0.000green:0.449blue:1.000alpha:1.000]range:[text.stringrangeOfString:@"more"]];

    [text yy_setTextHighlight:hirange:[text.stringrangeOfString:@"more"]];

    text.yy_font =_label.font;

    

    YYLabel *seeMore = [YYLabelnew];

    seeMore.attributedText = text;

    [seeMore sizeToFit];

    

    NSAttributedString *truncationToken = [NSAttributedStringyy_attachmentStringWithContent:seeMorecontentMode:UIViewContentModeCenterattachmentSize:seeMore.frame.sizealignToFont:text.yy_fontalignment:YYTextVerticalAlignmentCenter];

    _label.truncationToken = truncationToken;

}




展开阅读全文

没有更多推荐了,返回首页