Hello, I am two west, today to introduce YYLabel, YYTextView several powerful usage, today, YYText although said the author has not maintained, but it seems that there is no alternative library, its function is still very powerful, there is a problem to change on the line is not, alas, I hope the author will recover soon. Let me know in the comments. Thank you butter-fly music.163.com/#/song?id=4…

Add full text, more, details and so on at the end of YYLabel

YYLabel *contentL = [[YYLabel alloc] init]; contentL.font = [UIFont systemFontOfSize:15]; contentL.numberOfLines = 3; contentL.preferredMaxLayoutWidth = 300; [self.view addSubview:contentL]; [contentL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(100); make.left.mas_equalTo(16); make.width.mas_equalTo(300); }]; NSString * STR = @" I really want to become a butterfly, fly high on the breeze, now I just want to meet you as soon as possible, put aside all the worries, if you forget that it doesn't matter, there is no more, there is extra time to waste, it seems that something will appear in this clear sky "; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str]; attrStr.yy_font = contentL.font; contentL.attributedText = attrStr; / / add details Don't need to click event NSMutableAttributedString * moreAttrStr = [[NSMutableAttributedString alloc] initWithString: @ "details..."); [moreAttrStr yy_setColor: RGB (71, 154, 248) range:[moreAttrStr. String rangeOfString:@" details "]]; moreAttrStr.yy_font = contentL.font; contentL.truncationToken = moreAttrStr; / / add details Need to click event NSMutableAttributedString * moreAttrStr = [[NSMutableAttributedString alloc] initWithString: @ "details..."]. moreAttrStr.yy_font = contentL.font; [moreAttrStr yy_setTextHighlightRange:[moreAttrStr. String rangeOfString:@" "] Color: RGB (71, 154, 0) 248) backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect);}]; YYLabel *moreL = [[YYLabel alloc] init]; moreL.attributedText= moreAttrStr; [moreL sizeToFit]; NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:moreL contentMode:UIViewContentModeCenter attachmentSize:moreL.frame.size alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter]; contentL.truncationToken= truncationToken;Copy the code

Note the way to write the click event. If YYLabel is not created as a carrier, it cannot respond to the click event

Expand the usage



Sometimes the text will break lines in the middle, and the details at the end of the text will be close to each other instead of being parked at the end of the last line. Some designs like to be placed at the end of the line. Here’s a solution

Here’s how you do ittruncationToken“Details” text is set to transparent, and then add a label or button at the end of the last line to display, so that the “details” is displayed at the end of the middle line when the text is wrapped, or the “details” will not block the content when the text is wrapped at the end.

The code:

NSMutableAttributedString * moreAttrStr = [[NSMutableAttributedString alloc] initWithString: @ "details..."); [moreAttrStr yy_setColor:[UIColor clearColor] range:[moreAttrStr. String rangeOfString:@" detail "]]; moreAttrStr.yy_font = contentL.font; contentL.truncationToken = moreAttrStr; NSMutableAttributedString * copyAttrStr = [[NSMutableAttributedString alloc] initWithString: @ "details"); copyAttrStr.yy_color = rgb(71, 154, 248); copyAttrStr.yy_font = contentL.font; YYLabel *moreL = [[YYLabel alloc] init]; moreL.attributedText= copyAttrStr; [contentL addSubview:moreL]; [moreL mas_makeConstraints:^(MASConstraintMaker *make) {make.right.mas_equalto (0) make.bottom.mas_equalTo(0); }];Copy the code

The effect is as follows:

YYLabel Add emotiv image labels and customize views

UILabel *topL = [[UILabel alloc] init]; topL.font = [UIFont systemFontOfSize:11]; TopL. Text = @" topL "; topL.backgroundColor = [UIColor redColor]; topL.textColor = [UIColor whiteColor]; topL.layer.cornerRadius = 4; topL.layer.masksToBounds = YES; topL.textAlignment = NSTextAlignmentCenter; topL.frame = CGRectMake(0, 0, 32, 16); / / set size NSAttributedString * topAttr = [NSAttributedString yy_attachmentStringWithContent: topL contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(topL.frame.size.width+4, topL.frame.size.height) alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter]; [attrStr insertAttributedString:topAttr atIndex:0]; UIImage *image = [UIImage imageNamed:@"app_icon_fire_17"]; UIImageView *imageV = [[UIImageView alloc] init]; imageV.frame = CGRectMake(0, 0, image.size.width, image.size.height); imageV.image = image; NSAttributedString *imageAttr = [NSAttributedString yy_attachmentStringWithContent:imageV contentMode:UIViewContentModeScaleAspectFit attachmentSize:image.size alignToFont:contentL.font alignment:YYTextVerticalAlignmentCenter]; [attrStr appendAttributedString:imageAttr]; contentL.attributedText = attrStr;Copy the code

YYTextAttachment can be used to add UIView directly to rich text. It is very convenient to display images, emoticons, tags, and even you can customize content and paste it, just remember to set the size. Optionally, you can use YYAnimatedImageView to display giFs

YYTextView add @, link, as a whole

YYTextView *contentV = [[YYTextView alloc] init]; [self.view addSubview:contentV]; [contentV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(100); make.left.mas_equalTo(16); make.right.mas_equalTo(-16); make.height.mas_equalTo(300); }]; NSMutableAttributedString *allAttrStr = [[NSMutableAttributedString alloc] init]; NSArray * arr = @ [@ "@ the ancient beast," @ "@ gabe beast," @ "@ bhikkhu beast," @ "@ beetle beast," @ "@ subaru beast," @ "@ elder brother ma beast," @ "@ sparda beast," @ "beast" @ di road]. for (NSString *str in arr) { NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str]; attrStr.yy_font = [UIFont systemFontOfSize:14]; [attrStr yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:NO] range:attrStr.yy_rangeOfAll]; / / not delete confirmation directly delete [allAttrStr appendAttributedString: attrStr]; [allAttrStr yy_appendString:@" "]; } NSMutableAttributedString *linkAttrStr = [[NSMutableAttributedString alloc] initWithString:@"https://juejin.cn"]; linkAttrStr.yy_font = [UIFont systemFontOfSize:14]; linkAttrStr.yy_color = [UIColor blueColor]; [linkAttrStr yy_setTextBinding:[YYTextBinding bindingWithDeleteConfirm:YES] range:linkAttrStr.yy_rangeOfAll]; / / a delete confirmation [allAttrStr appendAttributedString: linkAttrStr]; contentV.attributedText = allAttrStr;Copy the code

YYTextBinding can be used to connect the parts you want into a whole, select the whole block, delete the whole block, is not very convenient. Incidentally, to match @ or links, one can use the regular expression NSRegularExpression to match

YYTextSimpleEmoticonParser simple expression parsing

NSMutableDictionary *mapper = [NSMutableDictionary new]; mapper[@":smile:"] = [UIImage imageNamed:@"smile"]; mapper[@":cry:"] = [UIImage imageNamed:@"cry"]; YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new]; parser.emoticonMapper = mapper; YYLabel *contentL = [[YYLabel alloc] init]; contentL.font = [UIFont systemFontOfSize:14]; Contentl. text = @" smile, smile, cry, smile." ; [self.view addSubview:contentL]; [contentL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(100); make.left.mas_equalTo(16); make.width.mas_equalTo(300); }];Copy the code

Finally added

There is no basic way to highlight what you click on. Oh, by the way, YYTextHighlight has a userInfo dictionary property that you can add fields to to pass along when you click on it.

YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor lightGrayColor]]; highlight.userInfo = @{@"uid":uid}; [attrStr yy_setTextHighlight:highlight range:NSMakeRange(0, str.length)]; contentL.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { __strong typeof(weakSelf) strongSelf = weakSelf; YYTextHighlight *highlight = [text yy_attribute:YYTextHighlightAttributeName atIndex:range.location] if (highlight) { // Click on the highlighted text NSDictionary *userInfo = highlight.userinfo; If (userInfo[@"uid"]){}}else{// click normal text}};Copy the code

Compare can use temporarily only think of so many, everybody if have other commonly used can tell me in the comment section duck ~~~