- 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];
}
}];
[mutableAttributeString enumerateAttributesInRange:NSMakeRange(0, mutableAttributeString.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _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];
}];
}
}];