iOS 富文本中插入表情圖片非常容易蜜氨,可是顯示的時候發(fā)現(xiàn)位置總是不在設(shè)想的位置炎滞,雖然經(jīng)過微調(diào)也可展示到居中的位置,但還是感覺有問題舵匾。找了好多資料終于發(fā)現(xiàn)了表情圖片位置計算規(guī)律俊抵。
- (NSMutableAttributedString *)getAttributedString:(NSString *)content image:(UIImage *)image font:(UIFont *)font {
NSMutableAttributedString *contentAtt = [[NSMutableAttributedString alloc] initWithString:content attributes:@{NSFontAttributeName : font}];
NSTextAttachment *imageMent = [[NSTextAttachment alloc] init];
imageMent.image = image;
CGFloat paddingTop = font.lineHeight - font.pointSize;
imageMent.bounds = CGRectMake(0, -paddingTop, font.lineHeight, font.lineHeight);
NSAttributedString *imageAtt = [NSAttributedString attributedStringWithAttachment:imageMent];
[contentAtt appendAttributedString:imageAtt];
return contentAtt;
}