代碼如下
方法調用如下
NSString * Str = @"非要等冰已裂疲憊時,非要等冰已裂疲憊時,愛你!";
self.attrobiuteLabel.attributedText = [self stringWithUIImage:Str];
整個方法如下
- (NSAttributedString *)stringWithUIImage:(NSString *) contentStr {
// 創建一個富文本
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
// 修改富文本中的不同文字的樣式
[attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
/**
添加圖片到指定的位置
*/
NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
// 表情圖片
attchImage.image = [UIImage imageNamed:@"jiedu"];
// 設置圖片大小
attchImage.bounds = CGRectMake(0, 0, 40, 15);
NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
[attriStr insertAttributedString:stringImage atIndex:2];
// 設置數字爲紅色
/*
[attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];
*/
//NSDictionary * attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
// NSForegroundColorAttributeName: [UIColor blueColor] };
//創建 NSAttributedString 並賦值
//_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];
NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
[attriStr addAttributes:attriBute range:NSMakeRange(5, 9)];
// 添加表情到最後一位
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情圖片
attch.image = [UIImage imageNamed:@"jiedu"];
// 設置圖片大小
attch.bounds = CGRectMake(0, 0, 40, 15);
// 創建帶有圖片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attriStr appendAttributedString:string];
return attriStr;
}
最後認識一下各個屬性的意思
// NSFontAttributeName 設置字體屬性,默認值:字體:Helvetica(Neue) 字號:12
// NSForegroundColorAttributeNam 設置字體顏色,取值爲 UIColor對象,默認值爲黑色
// NSBackgroundColorAttributeName 設置字體所在區域背景顏色,取值爲 UIColor對象,默認值爲nil, 透明色
// NSLigatureAttributeName 設置連體屬性,取值爲NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符
// NSKernAttributeName 設定字符間距,取值爲 NSNumber 對象(整數),正值間距加寬,負值間距變窄
// NSStrikethroughStyleAttributeName 設置刪除線,取值爲 NSNumber 對象(整數)
// NSStrikethroughColorAttributeName 設置刪除線顏色,取值爲 UIColor 對象,默認值爲黑色
// NSUnderlineStyleAttributeName 設置下劃線,取值爲 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似
// NSUnderlineColorAttributeName 設置下劃線顏色,取值爲 UIColor 對象,默認值爲黑色
// NSStrokeWidthAttributeName 設置筆畫寬度,取值爲 NSNumber 對象(整數),負值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值爲 UIColor 對象
// NSShadowAttributeName 設置陰影屬性,取值爲 NSShadow 對象
// NSTextEffectAttributeName 設置文本特殊效果,取值爲 NSString 對象,目前只有圖版印刷效果可用:
// NSBaselineOffsetAttributeName 設置基線偏移值,取值爲 NSNumber (float),正值上偏,負值下偏
// NSObliquenessAttributeName 設置字形傾斜度,取值爲 NSNumber (float),正值右傾,負值左傾
// NSExpansionAttributeName 設置文本橫向拉伸屬性,取值爲 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本
// NSWritingDirectionAttributeName 設置文字書寫方向,從左向右書寫或者從右向左書寫
// NSVerticalGlyphFormAttributeName 設置文字排版方向,取值爲 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本
// NSLinkAttributeName 設置鏈接屬性,點擊後調用瀏覽器打開指定URL地址
// NSAttachmentAttributeName 設置文本附件,取值爲NSTextAttachment對象,常用於文字圖片混排
// NSParagraphStyleAttributeName 設置文本段落排版格式,取值爲 NSParagraphStyle 對象