YYLabel富文本

  • YYLabel与UILabel对表情的支持
    YYLabel只支持YYTextAttachment,不支持系统默认的NSTextAttachment
// NSTextAttachment转换成YYTextAttachment
[mutableAttributeString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, mutableAttributeString.length) options:0 usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if ([value isKindOfClass:NSTextAttachment.class]) {
            NSTextAttachment *attachment = (NSTextAttachment *)value;
            NSAttributedString *replaceAttributeString = [NSMutableAttributedString yy_attachmentStringWithContent:attachment.image contentMode:UIViewContentModeScaleAspectFit attachmentSize:attachment.bounds.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [mutableAttributeString replaceCharactersInRange:range withAttributedString:replaceAttributeString];
        }
    }];
  • YYLabel高亮显示、点击事件
[mutableAttributeString enumerateAttributesInRange:NSMakeRange(0, mutableAttributeString.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        if (range.length > 0 && [attrs objectForKey:NSLinkAttributeName]) {
            [mutableAttributeString yy_setTextHighlightRange:range color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                NSAttributedString *subText = [text attributedSubstringFromRange:range];
                NSURL *link = [attrs objectForKey:NSLinkAttributeName];
            }];
        }
    }];

你可能感兴趣的:(YYLabel富文本)