ios項(xiàng)目中經(jīng)常需要顯示一些帶有特殊樣式的文本,比如說(shuō)帶有下劃線、刪除線、斜體、空心字體、背景色、陰影以及圖文混排(一種文字中夾雜圖片的顯示效果)。通常想要實(shí)現(xiàn)這些效果要使用到iOS的Foundation框架提供的NSAttributedString類,NSAttributedString類中有許多屬性,不同屬性對(duì)應(yīng)不同的文本樣式。本文主要對(duì)這些屬性做一個(gè)解釋說(shuō)明,并會(huì)結(jié)合實(shí)際代碼來(lái)應(yīng)用它們。
1. NSAttributedString屬性概覽表
| key | value | 說(shuō)明 |
|---|---|---|
| NSFontAttributeName | UIFont對(duì)象 | 字體大?。耗J(rèn)Helvetica(Neue) 12 |
| NSParagraphStyleAttributeName | NSParagraphStyle對(duì)象 | 文本字、行間距,對(duì)齊等:默認(rèn)defaultParagraphStyle |
| NSForegroundColorAttributeName | UIColor對(duì)象 | 字體顏色:默認(rèn)blackColor |
| NSBackgroundColorAttributeName | UIColor對(duì)象 | 背景色:默認(rèn)nil(無(wú)背景色) |
| NSLigatureAttributeName | 包含整數(shù)的NSNumber對(duì)象 | 連字符:ios中有0和1兩個(gè)值;0表示沒(méi)有連字符,而1是默認(rèn)的連字符 |
| NSKernAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對(duì)象 | 字符間距:默認(rèn)0(禁用) |
| NSStrikethroughStyleAttributeName | 包含整數(shù)的NSNumber對(duì)象 | 刪除線:默認(rèn)0(無(wú)刪除線) |
| NSUnderlineStyleAttributeName | 包含整數(shù)的NSNumber對(duì)象 | 下劃線:默認(rèn)0(無(wú)下劃線) |
| NSStrikethroughColorAttributeName | UIColor對(duì)象 | 刪除線顏色:默認(rèn) nil(和文字的 foregroundColor一致) |
| NSUnderlineColorAttributeName | UIColor對(duì)象 | 下劃線顏色:默認(rèn)nil(和文字的 foregroundColor一致) |
| NSStrokeColorAttributeName | UIColor對(duì)象 | 描邊顏色:nil(和文字的 foregroundColor一致) |
| NSStrokeWidthAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對(duì)象 | 描邊寬度:正值空心描邊,負(fù)值實(shí)心描邊,默認(rèn)0(不描邊) |
| NSShadowAttributeName | NSShadow對(duì)象 | 文本陰影:默認(rèn)nil(沒(méi)有陰影) |
| NSTextEffectAttributeName | NSString對(duì)象 | 文字效果:默認(rèn)nil(沒(méi)有文字效果) |
| NSAttachmentAttributeName | NSTextAttachment對(duì)象 | 附件(常用作圖文混排) :默認(rèn)nil(沒(méi)有附件) |
| NSLinkAttributeName | NSURL (優(yōu)先) 或 NSString對(duì)象 | 鏈接 |
| NSBaselineOffsetAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對(duì)象 | 基礎(chǔ)偏移量:正值向上偏移,負(fù)值向下偏移,默認(rèn)0(不偏移) |
| NSObliquenessAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對(duì)象 | 字體傾斜 :正值向右傾斜,負(fù)值向左傾斜, 默認(rèn)0(不傾斜) |
| NSExpansionAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對(duì)象 | 文本扁平化:正值橫向拉伸,負(fù)值橫向壓縮,默認(rèn)0(不拉伸) |
2. 屬性詳解及應(yīng)用(圖文混排比較特殊,會(huì)在第 3 部分單獨(dú)說(shuō)明)
NSMutableAttributedString 是 NSAttributedString 的子類,一般來(lái)說(shuō)我比較習(xí)慣使用NSMutableAttributedString來(lái)實(shí)現(xiàn)富文本,本文的例子也是采用NSMutableAttributedString來(lái)實(shí)現(xiàn)的,與NSAttributedString實(shí)現(xiàn)效果相比并無(wú)優(yōu)缺點(diǎn)之分,主要目的是講清楚這些屬性。
本文使用NSMutableAttributedString實(shí)現(xiàn)富文本,主要步驟分為以下三步,為避免下面解釋代碼時(shí)所添加的注釋不斷重復(fù),特在此說(shuō)明提前說(shuō)明一下:
-
調(diào)用 - (instancetype)initWithString:(NSString *)str 方法來(lái)創(chuàng)建 NSMutableAttributedString 實(shí)例
-
調(diào)用 - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range 方法添加所需的 Attribute 屬性,要注意range的范圍,所添加的屬性只對(duì)指定范圍內(nèi)的文字有效
-
給 label 賦值 : label.attributedText = attributedString;
2.1 NSFontAttributeName —— 字體大小
代碼 :
- (void)fontAttributeNameTest {
NSString *text = @"我是30號(hào)系統(tǒng)字體,你是15號(hào)Courier-BoldOblique字體";
// 1.創(chuàng)建NSMutableAttributedString實(shí)例
NSMutableAttributedString *fontAttributeNameStr = [[NSMutableAttributedString alloc]initWithString:text];
// 2.添加屬性
[fontAttributeNameStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 9)];
[fontAttributeNameStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:15] range:NSMakeRange(9, text.length - 9)];
// 3.給label賦值
self.label.attributedText = fontAttributeNameStr;
}
效果圖 :

2.2 NSParagraphStyleAttributeName —— 文本字、行間距,對(duì)齊等
代碼:
- (void)paragraphStyleAttributeNameTest {
NSString *text = @"我是一個(gè)很長(zhǎng)很長(zhǎng)很長(zhǎng)的文本,我的字間距是5,行間距是20,對(duì)齊方式為居中對(duì)齊。";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
// 創(chuàng)建NSMutableParagraphStyle實(shí)例
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5; //字間距5
paragraphStyle.paragraphSpacing = 20; //行間距是20
paragraphStyle.alignment = NSTextAlignmentCenter; //對(duì)齊方式為居中對(duì)齊
[attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
self.label.attributedText = attributeStr;
}
效果圖:

2.3 NSForegroundColorAttributeName —— 字體顏色
代碼:
- (void)foregroundColorAttributeNameTest {
NSString *text = @"你好呀,我默認(rèn)顏色是label的textColor,但是我現(xiàn)在要通過(guò)NSForegroundColorAttributeName屬性變成一個(gè)藍(lán)色了,看好嘍!";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(26, text.length - 26)];
self.label.attributedText = attributeStr;
}
效果圖:

2.4 NSBackgroundColorAttributeName —— 背景色
代碼:
- (void)backgroundColorAttributeNameTest {
NSString *text = @"我是一個(gè)紫色背景色的文本!";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
[attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, text.length)];
self.label.attributedText = attributeStr;
}
效果圖:

2.5 NSLigatureAttributeName —— 連字符
ios 中有 0 和 1 兩個(gè)值:0表示沒(méi)有連字符,而1是默認(rèn)的連字符。(一般對(duì)連筆寫的英文有效, 中文即使設(shè)置了連字符也很難表現(xiàn)出來(lái))。
代碼:
- (void)ligatureAttributeNameTest {
NSString *text = @"逗號(hào)前面的我是一個(gè)沒(méi)有連字符樣式的fl,逗號(hào)后面的你是一個(gè)帶連字符樣式的fl(你看后半句的漢字連字符樣式好難體現(xiàn)出來(lái)哦)";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName: @"futura" size: 20] range:NSMakeRange(0, text.length)];
// 設(shè)置文本前半句無(wú)連字符效果
[attributeStr addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:0] range:NSMakeRange(0, 19)];
// 設(shè)置文本后半句有連字符效果
[attributeStr addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(19, text.length - 19)];
self.label.attributedText = attributeStr;
}
效果圖:

2.6 NSKernAttributeName —— 字符間距
注意: 正值間距加寬,負(fù)值間距變窄,0表示默認(rèn)效果
代碼:
- (void)kernAttributeNameTest {
NSString *text = @"設(shè)置我的字間距為正值20有拉大效果,中間的你是正常效果,設(shè)置他的字間距為負(fù)值-5有減少效果";
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
[attributeStr addAttribute:NSKernAttributeName value:@20 range:NSMakeRange(0, 18)];
[attributeStr addAttribute:NSKernAttributeName value:@(-5) range:NSMakeRange(28, text.length - 28)];
self.label.attributedText = attributeStr;
}
效果圖:

----------------------------寫在設(shè)置文本的刪除線和下劃線之前----------------------
刪除線和下劃線的區(qū)別就是:刪除線是在文字中間顯示,下劃線是在文字的底部顯示;除這一點(diǎn)外,設(shè)置的可選值是一樣的,都是NSUnderlineStyle的枚舉常量
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09,
NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,
NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,
NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,
NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,
NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,
NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000
在 2.7 中會(huì)對(duì)他們進(jìn)行一個(gè)統(tǒng)一說(shuō)明。
--------------------------------------------------------------------------------------------------
2.7 NSStrikethroughStyleAttributeName —— 刪除線NSStrikethroughColorAttributeName —— 刪除線顏色NSUnderlineStyleAttributeName —— 下劃線NSUnderlineColorAttributeName —— 下劃線顏色
代碼:
- (void)strikethroughStyleAndUnderlineStyleTest {
NSArray *textArr = @[@"NSUnderlineStyleNone樣式", @"NSUnderlineStyleSingle樣式", @"NSUnderlineStyleThick樣式", @"NSUnderlineStyleDouble樣式"];
NSArray *colorArr = @[[UIColor blueColor], [UIColor redColor], [UIColor purpleColor], [UIColor whiteColor]];
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] init];
//依次為每個(gè)字符串?dāng)?shù)組中的字符添加不同樣式和不同顏色的刪除線、下劃線
for (int i = 0; i < textArr.count; i++) {
NSMutableAttributedString *singleAttributeStr = [[NSMutableAttributedString alloc]initWithString:textArr[i]];
//設(shè)置刪除線樣式
[singleAttributeStr addAttribute:NSStrikethroughStyleAttributeName value:@(i) range:NSMakeRange(0, ((NSString *)textArr[i]).length)];
//設(shè)置刪除線顏色
[singleAttributeStr addAttribute:NSStrikethroughColorAttributeName value:colorArr[i] range:NSMakeRange(0, ((NSString *)textArr[i]).length)];
//設(shè)置下劃線樣式
[singleAttributeStr addAttribute:NSUnderlineStyleAttributeName value:@(i) range:NSMakeRange(0, ((NSString *)textArr[i]).length)];
//設(shè)置下劃線顏色
[singleAttributeStr addAttribute:NSUnderlineColorAttributeName value:colorArr[i] range:NSMakeRange(0, ((NSString *)textArr[i]).length)];
[attributeStr appendAttributedString:singleAttributeStr];
}
self.label.attributedText = attributeStr;
}
效果圖:

2.8 NSStrokeColorAttributeName —— 描邊顏色NSStrokeWidthAttributeName —— 描邊寬度
注意事項(xiàng):
- 描邊顏色要搭配非0的描邊寬度才會(huì)生效,如果只設(shè)置了描邊顏色,描邊寬度為0,則沒(méi)有描邊效果
- 描邊寬度是正數(shù),會(huì)對(duì)文字進(jìn)行描邊,但文字中心不填充( 一種經(jīng)典的空心文本樣式是在該值為3.0)
- 描邊寬度是負(fù)數(shù),會(huì)對(duì)文字進(jìn)行描邊,而且會(huì)同時(shí)對(duì)文字中心進(jìn)行填充(填充的顏色為文字本來(lái)的字體顏色)
代碼:
- (void)strokeTest {
// 給第一個(gè)label只設(shè)置描邊顏色
NSString *text1 = @"只設(shè)置描邊顏色,沒(méi)有設(shè)置描邊寬度(默認(rèn)為0),沒(méi)有效果";
NSMutableAttributedString *attributeStr1 = [[NSMutableAttributedString alloc] initWithString:text1 ];
[attributeStr1 addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, text1.length)];
self.label1.attributedText = attributeStr1;
// 給第二個(gè)label設(shè)置描邊寬度為正數(shù)3,不設(shè)置描邊顏色
NSString *text2 = @"將描邊寬度設(shè)置為正數(shù)3,無(wú)描邊顏色,具有空心效果哦,此時(shí)描邊顏色默認(rèn)成字體本來(lái)的顏色!";
NSMutableAttributedString *attributeStr2 = [[NSMutableAttributedString alloc] initWithString:text2 ];
[attributeStr2 addAttribute:NSStrokeWidthAttributeName value:@(3) range:NSMakeRange(0, text2.length)];
self.label2.attributedText = attributeStr2;
// 給第三個(gè)label設(shè)置描邊寬度為正數(shù)3,描邊顏色為紅色
NSString *text3 = @"將描邊寬度設(shè)置為正數(shù)3,描邊顏色為紅色,具有空心效果哦,因?yàn)檎龜?shù)不對(duì)文字內(nèi)部進(jìn)行填充!";
NSMutableAttributedString *attributeStr3 = [[NSMutableAttributedString alloc] initWithString:text3 ];
[attributeStr3 addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text3.length)];
[attributeStr3 addAttribute:NSStrokeWidthAttributeName value:@(3) range:NSMakeRange(0, text3.length)];
self.label.attributedText = attributeStr3;
// 給第四個(gè)label設(shè)置描邊寬度為負(fù)數(shù)-3,描邊顏色為紫色
NSString *text4 = @"將描邊寬度設(shè)置為負(fù)數(shù)-3,又設(shè)置描邊顏色,無(wú)空心效果,因?yàn)樨?fù)數(shù)會(huì)對(duì)文字內(nèi)部進(jìn)行填充!";
NSMutableAttributedString *attributeStr4 = [[NSMutableAttributedString alloc] initWithString:text4 ];
[attributeStr4 addAttribute:NSStrokeColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, text4.length)];
[attributeStr4 addAttribute:NSStrokeWidthAttributeName value:@(-3) range:NSMakeRange(0, text4.length)];
self.label3.attributedText = attributeStr4;
}
效果圖:

2.9 NSShadowAttributeName —— 文本陰影
代碼:
- (void)shadowTest {
NSString *text = @"一個(gè)有陰影的文本!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
// 創(chuàng)建NSShadow實(shí)例
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor purpleColor];
shadow.shadowBlurRadius = 3.0;
shadow.shadowOffset = CGSizeMake(0, 0.8);
// 添加屬性
[attributeStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
self.label.attributedText = attributeStr;
}
效果圖:

2.10 NSTextEffectAttributeName —— 文字效果
代碼:
- (void)textEffectAttributeTest {
NSString *text = @"我是沒(méi)有文字效果的,你是有文字效果的!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
[attributeStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(10, text.length - 10)];
self.label.attributedText = attributeStr;
}
效果圖:

2.11 NSLinkAttributeName —— 鏈接
注意:UILabel無(wú)法使用該屬性, 但UITextView 控件可以使用,所以下面關(guān)于 NSLinkAttributeName 屬性的代碼也是使用 UITextView 來(lái)測(cè)試的。
代碼:
// 注意:跳轉(zhuǎn)鏈接要實(shí)現(xiàn)UITextView的這個(gè)委托方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange {
return YES;
}
- (void)linkAttributeTest {
NSString *text = @"點(diǎn)我跳轉(zhuǎn)到百度哦!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
[attributeStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, text.length)];
self.textView.attributedText = attributeStr;
}
效果圖:


2.12 NSBaselineOffsetAttributeName —— 基礎(chǔ)偏移量
注意:正值向上偏移,負(fù)值向下偏移,默認(rèn)0(不偏移)
代碼:
- (void)baselineOffsetAttributeName {
NSString *text = @"正值向上偏移,無(wú)偏移效果,負(fù)值向下偏移!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
[attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(10) range:NSMakeRange(0, 7)];
[attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(-10) range:NSMakeRange(13, text.length - 13)];
self.label3.attributedText = attributeStr;
}
效果圖:

2.13 NSObliquenessAttributeName —— 字體傾斜
注意:正值向右傾斜,負(fù)值向左傾斜, 默認(rèn)0(不傾斜)
代碼:
- (void)obliquenessTest {
NSString *text = @"正值向右傾斜,無(wú)偏移效果,負(fù)值向左傾斜!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
[attributeStr addAttribute:NSObliquenessAttributeName value:@(1) range:NSMakeRange(0, 7)];
[attributeStr addAttribute:NSObliquenessAttributeName value:@(-1) range:NSMakeRange(13, text.length - 13)];
self.label.attributedText = attributeStr;
}
效果圖:

2.14 NSExpansionAttributeName —— 文本扁平化(橫向拉伸)
注意:正值橫向拉伸,負(fù)值橫向壓縮,默認(rèn)0(不拉伸)
代碼:
- (void)expansionAttributeTest {
NSString *text = @"正值橫向拉伸,無(wú)扁平效果,負(fù)值橫向壓縮!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
[attributeStr addAttribute:NSExpansionAttributeName value:@(0.8) range:NSMakeRange(0, 7)];
[attributeStr addAttribute:NSExpansionAttributeName value:@(-0.8) range:NSMakeRange(13, text.length - 13)];
self.label.attributedText = attributeStr;
}
效果圖:

3. 圖文混排
應(yīng)用場(chǎng)景:如果界面需要顯示一串從后臺(tái)拿到的一個(gè)文本,這個(gè)文本有長(zhǎng)有短,要求文本后面要拼接一個(gè)圖片,而且如果文本長(zhǎng)度為超過(guò)一行,圖片要跟在文本的最后面。此時(shí)就要求圖片似乎作為一個(gè)文本一樣,可以拼接在后臺(tái)拿到的文本后面。類似這樣的:



這樣的效果還是通過(guò)NSAttributedString類實(shí)現(xiàn)的。步驟:
- 使用文本生成指定樣式的AttributedString;
- 使用圖片生成AttributedString;
- 將生成的AttributedString拼接起來(lái)。
- 參考代碼:
// 1. 由文本生成attributedString
+ (NSAttributedString *)attributedStringWithText:(NSString *)text textColor:(UIColor *)color textFont:(UIFont *)font hasUnderlineStyle:(BOOL)hasUnderLineStyle lineSpacing:(float)line paragraphSpacing:(float)paragraph {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
NSRange range = NSMakeRange(0, text.length);
[paragraphStyle setLineSpacing:line];
[paragraphStyle setParagraphSpacing:paragraph];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
[attributedString addAttribute:NSFontAttributeName value:font range:range];
if (hasUnderLineStyle) {
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
}
return attributedString;
}
// 2. 由圖片生成attributedString
+ (NSAttributedString *)attributedStringWithImage:(UIImage *)image imageBounds:(CGRect)bounds {
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = image;
textAttachment.bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
NSAttributedString *attachmentAttributedString = [NSAttributedString attributedStringWithAttachment:textAttachment];
return attachmentAttributedString;
}
// 3. 多個(gè)AttributedString拼接成一個(gè)resultAttributedString
+ (NSAttributedString *)jointAttributedStringWithItems:(NSArray *)items {
NSMutableAttributedString *resultAttributedString = [[NSMutableAttributedString alloc] init];
for (int i = 0; i < items.count; i++) {
if ([items[i] isKindOfClass:[NSAttributedString class]]) {
[resultAttributedString appendAttributedString:items[i]];
}
}
return resultAttributedString;
}
圖片在文字之后的調(diào)用方式:
- (void)initializeLabel:(UILabel *)label withText:(NSString *)text {
// 傳健文本的attributedString
NSAttributedString *textAttributedString = [UILabel attributedStringWithText:text textColor:[UIColor blackColor] textFont:[UIFont systemFontOfSize:20] hasUnderlineStyle:NO lineSpacing:6 paragraphSpacing:16];
// 創(chuàng)建圖片的attributedString
NSAttributedString *imageAttributedString = [UILabel attributedStringWithImage:[UIImage imageNamed:@"lovebook.png"] imageBounds:CGRectMake(0, -5, 109, 28)];
// 將文本和圖片的attributedString拼接成一個(gè)resultAttributedString
NSAttributedString *resultAttributedString = [UILabel jointAttributedStringWithItems:@[textAttributedString, imageAttributedString]];
// 將resultAttributedString賦值給label
label.attributedText = resultAttributedString;
}
其他圖片在文字前、在文字中的調(diào)用方式和上面的代碼類似,這里不再贅述!
PS: 關(guān)于圖文混排參考這篇文章, 寫的很好,收獲很大,感謝分享!
最后附一張屬性表圖:
