NSMutableAttributedString *vipStr = [[NSMutableAttributedString alloc]initWithString:@"點擊解鎖【超級方法】"]; UIImage *vipImage = [UIImage imageNamed:@"WordDetailNor解鎖"]; NSTextAttachment *vipImageAttachment = [[NSTextAttachment alloc]init]; vipImageAttachment.image = vipImage; vipImageAttachment.bounds = CGRectMake(0, 0, 13, 15); NSAttributedString *vipImageAttrStr = [NSAttributedString attributedStringWithAttachment:vipImageAttachment]; [vipStr insertAttributedString:vipImageAttrStr atIndex:0]; //設置字體和設置字體的範圍 [vipStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0f] range:NSMakeRange(0, vipStr.length)]; [self.noteLabel setAttributedText:vipStr];
效果圖:字體
修改spa
vipImageAttachment.bounds = CGRectMake(-5, 0, 13, 15);
會把圖片前移、可是圖片顯示不全3d
效果圖:code
新增代碼blog
[vipStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, vipStr.length)];
效果並不理想,只有文字受到NSKernAttributeName
屬性影響。圖片間距保持原樣。圖片
//設置空格文本 [vipStr insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1]; //設置間距 [vipStr addAttribute:NSKernAttributeName value:@(8) range:NSMakeRange(1,1)];
效果圖:ip
最終採用最後一個騷操做、解決問題!it