NSMutableAttributedString *attrString = ({ // words NSAttributedString *leftString = [[NSAttributedString alloc] initWithString:@"好" attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]}]; // image NSAttributedString *attachString = ({ UIImage *image = [UIImage imageNamed:@"项目标签"]; NSTextAttachment *attach = [NSTextAttachment new]; attach.image = image; [NSAttributedString attributedStringWithAttachment:attach]; }); NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithAttributedString:leftString]; [attrString appendAttributedString:attachString]; // set line-height NSMutableParagraphStyle *style = [NSMutableParagraphStyle new]; style.maximumLineHeight = 22.5; style.minimumLineHeight = 22.5; [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attrString.length)]; // set baselineOffset [attrString addAttribute:NSBaselineOffsetAttributeName value:@0 range:NSMakeRange(0, attrString.length)]; attrString; });
If there is some words before the image attachment, the baselineOffset of image's attributedString can not be set to 0.