曾經做項目時遇到這樣一個需求:用 UITextView 展示一段電影簡介。文字很長,字號不得不調小,行間距和段間距也壓得很緊,一大段擠在一起實在看不下去。想把行距撐開一些,結果翻遍 Xcode 裡 UITextView 的所有屬性,才發現它居然沒有提供調整行距的接口。
抱著試試的心態上網一查,才知道 iOS 的文字排版能力其實相當強大,只不過入口不在這些常用控件上。
在 iOS(以及 Mac OS X)中把字符串畫到屏幕上,靠的是控件。iOS 用 UIKit 框架,Mac OS X 用 AppKit 框架。UIKit 裡常用來顯示文本的控件有三個:
這些控件對文本的呈現方式比較單一,通常只能控制字體、字號、顏色、粗體、斜體之類的基礎樣式;一旦涉及行距、字距、段落這類排版級需求,就無能為力了。
這時就不得不提到 CoreText.framework。它是 iOS 3.2+ 與 OS X 10.5+ 起提供的文本引擎,能對文本格式和佈局做非常精細的控制,並把 UIKit 與 Core Graphics/Quartz 結合起來:
遺憾的是 CoreText 體系龐大、學習成本高,非特殊需求很少有人直接用。
iOS 6 之後情況有所改觀:UITextField、UITextView 和 UILabel 相繼支持 AttributedString,行距、字距、段落控制不必再動用複雜的 CoreText。iOS 7 又引入了 TextKit——一個快速而現代化的排版與渲染引擎。它沒有新增類,而是在既有的文本控件上做封裝,可以直接在 UILabel、UITextField、UITextView 中使用。
蘋果並不是要用 TextKit 取代 CoreText。CoreText 屬於底層的高性能方案,當你需要把文本直接渲染到圖形上下文時,性能和易用性上它仍是最佳選擇;而如果只是想用系統控件排版文字,TextKit 提供的 API 顯然更方便。
不過本文要談的既不是 CoreText 也不是 TextKit,因為我們的需求用一個小巧的 AttributedString 就能搞定。
和 NSString 類似,AttributedString 分為 NSAttributedString 與 NSMutableAttributedString。不同的是,它多了一個 Attribute(屬性)的概念:一個 attributed string 可以攜帶多組屬性,每組屬性各自對應一段字符區域,這段區域用 NSRange 描述。
先初始化一個 NSMutableAttributedString,不斷往裡追加文本樣式,最後賦給控件的 attributedText。適合文本量不大、但需要分段精細控制的場景。
NSString *originStr = @"Hello,中秋節!";
// 創建可變的 attributed string
NSMutableAttributedString *attributedStr01 =
[[NSMutableAttributedString alloc] initWithString:originStr];
// 全部字符設置字體為 Zapfino,字號 15
[attributedStr01 addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"Zapfino" size:15]
range:NSMakeRange(0, originStr.length)];
// 前 4 個字符設為藍色
[attributedStr01 addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(0, 4)];
// 第 5、6、7 個字符設為紅色
[attributedStr01 addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(4, 3)];
_label01.attributedText = attributedStr01;
先構造屬性字典,再和文本一起初始化。適合文本量較大、整體樣式統一的場景,例如從文件讀進來的大段內容。
NSDictionary *attrDict = @{
NSFontAttributeName: [UIFont fontWithName:@"Zapfino" size:15],
NSForegroundColorAttributeName: [UIColor blueColor]
};
_label02.attributedText = [[NSAttributedString alloc]
initWithString:originStr attributes:attrDict];
對比可知:方式一擅長複雜格式,但屬性設置繁瑣;方式二簡潔明瞭,卻不擅長多樣化的分段控制。當然,「不擅長」並不等於「做不到」,把文本切片後分別賦值、最後拼接即可:
// 第一段:前 4 個字符,藍色
NSDictionary *attrDict1 = @{
NSFontAttributeName: [UIFont fontWithName:@"Zapfino" size:15],
NSForegroundColorAttributeName: [UIColor blueColor]
};
NSAttributedString *attrStr1 = [[NSAttributedString alloc]
initWithString:[originStr substringWithRange:NSMakeRange(0, 4)]
attributes:attrDict1];
// 第二段:第 5~7 個字符,紅色
NSDictionary *attrDict2 = @{
NSFontAttributeName: [UIFont fontWithName:@"Zapfino" size:15],
NSForegroundColorAttributeName: [UIColor redColor]
};
NSAttributedString *attrStr2 = [[NSAttributedString alloc]
initWithString:[originStr substringWithRange:NSMakeRange(4, 3)]
attributes:attrDict2];
// 第三段:剩餘字符,黑色
NSDictionary *attrDict3 = @{
NSFontAttributeName: [UIFont fontWithName:@"Zapfino" size:15],
NSForegroundColorAttributeName: [UIColor blackColor]
};
NSAttributedString *attrStr3 = [[NSAttributedString alloc]
initWithString:[originStr substringWithRange:NSMakeRange(7, originStr.length - 4 - 3)]
attributes:attrDict3];
// 合併
NSMutableAttributedString *attributedStr03 =
[[NSMutableAttributedString alloc] initWithAttributedString:attrStr1];
[attributedStr03 appendAttributedString:attrStr2];
[attributedStr03 appendAttributedString:attrStr3];
_label03.attributedText = attributedStr03;
搞清楚了創建方式,接下來看 AttributedString 到底能設置哪些屬性。常用的一共有 21 個:
// 字體,默認 Helvetica(Neue) 12 NSFontAttributeName // 字體顏色,取值 UIColor,默認黑色 NSForegroundColorAttributeName // 文本所在區域的背景色,取值 UIColor,默認 nil(透明) NSBackgroundColorAttributeName // 連體字符,取值 NSNumber,0 無連體,1 使用默認連體 NSLigatureAttributeName // 字符間距,取值 NSNumber,正值加寬,負值收窄 NSKernAttributeName // 刪除線,取值 NSNumber NSStrikethroughStyleAttributeName // 刪除線顏色,取值 UIColor,默認黑色 NSStrikethroughColorAttributeName // 下劃線,取值 NSNumber,枚舉 NSUnderlineStyle NSUnderlineStyleAttributeName // 下劃線顏色,取值 UIColor,默認黑色 NSUnderlineColorAttributeName // 筆畫寬度,取值 NSNumber,負值填充、正值中空 NSStrokeWidthAttributeName // 填充部分顏色(不是字體顏色),取值 UIColor NSStrokeColorAttributeName // 陰影,取值 NSShadow NSShadowAttributeName // 文本特殊效果,取值 NSString,目前僅支持印刷效果 NSTextEffectAttributeName // 基線偏移,取值 NSNumber(float),正值上偏,負值下偏 NSBaselineOffsetAttributeName // 字形傾斜度,取值 NSNumber(float),正值右傾,負值左傾 NSObliquenessAttributeName // 橫向拉伸,取值 NSNumber(float),正值拉伸,負值壓縮 NSExpansionAttributeName // 書寫方向,可設為從左到右或從右到左 NSWritingDirectionAttributeName // 排版方向,0 橫排,1 豎排 NSVerticalGlyphFormAttributeName // 鏈接,點擊後調用瀏覽器打開指定 URL NSLinkAttributeName // 文本附件,取值 NSTextAttachment,常用於圖文混排 NSAttachmentAttributeName // 段落排版格式,取值 NSParagraphStyle NSParagraphStyleAttributeName
NSDictionary *attrDict1 = @{NSForegroundColorAttributeName: [UIColor redColor]};
NSDictionary *attrDict2 = @{NSForegroundColorAttributeName: [UIColor blueColor]};
NSDictionary *attrDict3 = @{NSForegroundColorAttributeName: [UIColor orangeColor]};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
注意:NSForegroundColorAttributeName 設置的顏色與 UILabel 的 textColor 屬性地位相等,誰最後賦值,最終就顯示誰的顏色。
NSDictionary *attrDict4 = @{NSBackgroundColorAttributeName: [UIColor orangeColor]};
NSDictionary *attrDict5 = @{NSBackgroundColorAttributeName: [UIColor redColor]};
NSDictionary *attrDict6 = @{NSBackgroundColorAttributeName: [UIColor cyanColor]};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict4];
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict5];
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict6];
仔細觀察會發現一個問題:明明沒有關閉 NSForegroundColorAttributeName,運行結果卻是默認的黑色。這說明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的優先級是相等的,後賦值者會沖掉前面的效果。把兩段代碼的順序交換,顯示結果也會跟著互換。
不過 textColor 屬性是可以與 NSBackgroundColorAttributeName 疊加的:
_label01.textColor = [UIColor greenColor]; _label02.textColor = [UIColor yellowColor]; _label03.textColor = [UIColor blueColor]; // 隨後再設置前景色與背景色的 attributedText // ...
雖然 textColor 在 NSFontAttributeName 之前賦值,但由於前景色效果被背景色沖掉,最終顯示的是 textColor 的顏色。
// 0 表示不使用連體,1 表示使用默認連體(iOS 不支持 2)
NSString *ligatureStr = @"flush";
NSDictionary *attrDict1 = @{
NSLigatureAttributeName: [NSNumber numberWithInt:0],
NSFontAttributeName: [UIFont fontWithName:@"futura" size:30]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:ligatureStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSLigatureAttributeName: @(1),
NSFontAttributeName: [UIFont fontWithName:@"futura" size:30]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:ligatureStr attributes:attrDict2];
因為要展示連體效果,這裡把帶中文的字符串換成了 flush。屬性值必須是 NSNumber 對象,不能直接塞整數,除了 [NSNumber numberWithInt:],也可以簡寫成 @(1)。注意观察字母 f 和 l 之間的變化。
連寫頗有一種「藝術字」的味道:當 f 和 l 以組合字形(glyph)繪製時,視覺上確實更順眼。但並非所有字符都有組合符號,實際上只有部分字體中的特定組合(如 f+l、f+i)才具備這種效果。
NSDictionary *attrDict1 = @{
NSKernAttributeName: @(-3),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSKernAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSKernAttributeName: @(10),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
// NSUnderlineStyleNone 不加刪除線
// NSUnderlineStyleSingle 細單實線
// NSUnderlineStyleThick 粗單實線
// NSUnderlineStyleDouble 細雙實線
NSDictionary *attrDict1 = @{
NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
幾點提醒:枚舉常量的本質仍是整數,必須先轉成 NSNumber;刪除線與下劃線共用同一套枚舉常量;iOS 目前實際生效的只有上面四種,頭文件裡能看到更多取值但並無效果。此外,中英文的刪除線位置略有差異。
有意思的是,除了這四種枚舉,刪除線其實還接受其他整數值:取 0~7 時為單實線,數值越大線越粗;取 9~15 時為雙實線,同理越粗越明顯。
NSDictionary *attrDict1 = @{
NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSStrikethroughStyleAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSStrikethroughStyleAttributeName: @(7),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
NSDictionary *attrDict1 = @{
NSStrikethroughColorAttributeName: [UIColor blueColor],
NSStrikethroughStyleAttributeName: @(1),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSStrikethroughColorAttributeName: [UIColor orangeColor],
NSStrikethroughStyleAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSStrikethroughColorAttributeName: [UIColor greenColor],
NSStrikethroughStyleAttributeName: @(7),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
下劃線除了線條位置與刪除線不同,其餘設置方式完全可以照搬。
NSDictionary *attrDict1 = @{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
NSDictionary *attrDict1 = @{
NSUnderlineColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSUnderlineColorAttributeName: [UIColor orangeColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSUnderlineColorAttributeName: [UIColor greenColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
NSFontAttributeName: [UIFont systemFontOfSize:20]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
NSDictionary *attrDict1 = @{
NSStrokeWidthAttributeName: @(-3),
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSStrokeWidthAttributeName: @(0),
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSStrokeWidthAttributeName: @(3),
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
負值為填充效果,正值為中空(描邊)效果。
NSDictionary *attrDict1 = @{
NSStrokeWidthAttributeName: @(-3),
NSStrokeColorAttributeName: [UIColor orangeColor],
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label01.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict1];
NSDictionary *attrDict2 = @{
NSStrokeWidthAttributeName: @(0),
NSStrokeColorAttributeName: [UIColor blueColor],
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label02.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict2];
NSDictionary *attrDict3 = @{
NSStrokeWidthAttributeName: @(3),
NSStrokeColorAttributeName: [UIColor greenColor],
NSFontAttributeName: [UIFont systemFontOfSize:30]
};
_label03.attributedText = [[NSAttributedString alloc] initWithString:originStr attributes:attrDict3];
要注意,NSStrokeColorAttributeName 描述的是填充部分的顏色,而不是字體顏色。
剩下像 NSShadowAttributeName、NSBaselineOffsetAttributeName、NSObliquenessAttributeName、NSParagraphStyleAttributeName、NSAttachmentAttributeName 等屬性,用法與上面完全一致,都是往字典裡塞對應類型的對象即可,這裡不再逐一展開。
總的來說,只要掌握了 AttributedString,行距、段距、字距這些過去要動用 CoreText 才能搞定的問題,幾行代碼就能解決。