java中向label末尾加文字_展开label,利用YYText实现文字显示不完末尾添加全文

#import "ViewController.h"

#import "YYLabel.h"

#import "NSAttributedString+YYText.h"

#import

@interfaceViewController ()

@property (nonatomic,strong) YYLabel*label;;@end

@implementationViewController- (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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值