iOS-展開label,利用YYText實(shí)現(xiàn)文字顯示不完末尾添加全文、更多、省略號

image.png

操作

先github下載<YYText>文件導(dǎo)入,

代碼如下:

#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當(dāng)前的群無多群無多群無多群無多群無多群多群無多群無多群無多群無多群多群多群多群當(dāng)前的群無多群多群無多群多群多群多群多群多群多群多群的權(quán)威的權(quán)威的期望多無群多群無多群多群多群多群無多群無多群無多群無多群無多群無多群多群無多群無多群多群無多群多群無多無多無群多多群無多群多群多群多群無多群多無!";

    [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) { // 點(diǎn)擊全文回調(diào) 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;
}

來自:https://www.cnblogs.com/xuzb/p/8968611.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容