iOS 6+ 引入屬性字串,支援同一字串不同範圍應用不同屬性 (字體、顏色、下劃線、段落樣式等)。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 50)];
label.numberOfLines = 0;
label.text = @"歡迎使用 MyApp,點擊這裡登入";
// 1. 建立可變屬性字串
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:label.text];
// 2. 設置「MyApp」為藍色粗體 (假設在第 7-10 字元)
NSRange appRange = NSMakeRange(6, 4); // 索引從 0 開始
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:appRange];
[attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:appRange];
// 3. 設置「這裡」為紅色、底線 (假設第 13-14 字元)
NSRange linkRange = NSMakeRange(12, 2);
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:linkRange];
[attrStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:linkRange];
// 4. 應用到 Label
label.attributedText = attrStr;
// UILabel+RichText.h
@interface UILabel (RichText)
- (void)setText:(NSString *)text
font:(UIFont *)font
textColor:(UIColor *)color
highlightRanges:(NSArray *)ranges
highlightColor:(UIColor *)highlightColor
highlightFont:(UIFont *)highlightFont;
@end
// UILabel+RichText.m
@implementation UILabel (RichText)
- (void)setText:(NSString *)text
font:(UIFont *)font
textColor:(UIColor *)color
highlightRanges:(NSArray *)ranges
highlightColor:(UIColor *)highlightColor
highlightFont:(UIFont *)highlightFont {
self.font = font;
self.textColor = color;
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
// 基礎屬性
[attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
[attrStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, text.length)];
// 高亮範圍屬性
for (NSValue *rangeValue in ranges) {
NSRange range = [rangeValue rangeValue];
if (range.location + range.length <= text.length) {
if (highlightFont) {
[attrStr addAttribute:NSFontAttributeName value:highlightFont range:range];
}
if (highlightColor) {
[attrStr addAttribute:NSForegroundColorAttributeName value:highlightColor range:range];
}
}
}
self.attributedText = attrStr;
}
@end
// 使用範例
[label setText:@"餘額:¥1,234.56 元"
font:[UIFont systemFontOfSize:14]
textColor:[UIColor darkGrayColor]
highlightRanges:@[[NSValue valueWithRange:NSMakeRange(3, 9)]] // ¥1,234.56
highlightColor:[UIColor redColor]
highlightFont:[UIFont boldSystemFontOfSize:16]];
| 屬性 | iOS 7-10 鍵名 | iOS 11+ 鍵名 | 值類型 | 說明 |
|---|---|---|---|---|
| 字體 | NSFontAttributeName | NSAttributedString.Key.font | UIFont | |
| 前景色 | NSForegroundColorAttributeName | .foregroundColor | UIColor | |
| 背景色 | NSBackgroundColorAttributeName | .backgroundColor | UIColor | 高亮背景 |
| 下劃線 | NSUnderlineStyleAttributeName | .underlineStyle | NSNumber (NSUnderlineStyle) | 單線/雙線/樣式 |
| 刪除線 | NSStrikethroughStyleAttributeName | .strikethroughStyle | NSNumber | |
| 陰影 | NSShadowAttributeName | .shadow | NSShadow | |
| 段落樣式 | NSParagraphStyleAttributeName | .paragraphStyle | NSMutableParagraphStyle | 行距、對齊、縮進 |
| 連結 | NSLinkAttributeName | .link | NSURL | 配合 UITextView 可點擊 |
| 附件 | NSAttachmentAttributeName | .attachment | NSTextAttachment | 圖片混排 |
| 基線偏移 | NSBaselineOffsetAttributeName | .baselineOffset | NSNumber (float) | 上標/下標 |
| 字距 | NSKernAttributeName | .kern | NSNumber (float) | 字符間距 |
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 200, 280, 100)];
textView.editable = NO;
textView.selectable = YES; // 關鍵:可選中才能點擊連結
textView.dataDetectorTypes = UIDataDetectorTypeLink; // 自動檢測 URL/電話
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"查看服務條款與隱私政策"];
[attr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://example.com/terms"] range:NSMakeRange(2, 5)]; // "服務條款"
[attr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"https://example.com/privacy"] range:NSMakeRange(9, 5)]; // "隱私政策"
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2, 5)];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(9, 5)];
textView.attributedText = attr;
// Delegate 處理點擊 (可選,阻止 Safari 彈出)
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
// 自定義處理:推入 WebView、原生頁面等
return NO; // 阻止默認 Safari 打開
}
NSString *html = @"重要:請閱讀條款";
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *attr = [[NSAttributedString alloc] initWithData:data
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
label.attributedText = attr;
// 注意:HTML 解析較慢,大量使用請背景線程預解析快取