我想在同一個UILabel中使用多種字體顏色。我不知道這是否可能。我真的不這麼認爲,但也許你們中有些人有一個聰明的解決方案呢?也許像stringWithFormat
。 [NSString stringWithFormatAndColor: @"Text with color: %@ %@", text, color, text, color]
是否有可能在同一UILabel中有多種顏色
此圖片說明什麼,我試圖完成:
我想在同一個UILabel中使用多種字體顏色。我不知道這是否可能。我真的不這麼認爲,但也許你們中有些人有一個聰明的解決方案呢?也許像stringWithFormat
。 [NSString stringWithFormatAndColor: @"Text with color: %@ %@", text, color, text, color]
是否有可能在同一UILabel中有多種顏色
此圖片說明什麼,我試圖完成:
您可以NSAttributedSting實現這一目標。一個易於使用的替換UILabels支持屬性字符串是TTTAtributedLabel或OHAttributedLabel
根據我的經驗,使用NSMutableAttributedStrings並逐步構建起來更容易。
NSMutableAttributedString *attrStr = [NSMutableAttributedString attributedStringWithString:@""];
NSMutableAttributedString *a = [NSMutableAttributedString attributedStringWithString:@"This is "];
[a setTextColor:aColorObj];
NSMutableAttributedString *b = [NSMutableAttributedString attributedStringWithString:@"only one "];
[b setTextColor:bColorObj];
NSMutableAttributedString *c = [NSMutableAttributedString attributedStringWithString:@"Label"];
[c setTextColor:cColorObj];
[attrStr appendAttributedString:a];
[attrStr appendAttributedString:b];
[attrStr appendAttributedString:c];
OHAttributedLabel *attributedTextLabel = [[OHAttributedLabel] initWithFrame:frame]
[attributedTextLabel setAttributedText:attrStr];
真棒,謝謝! – picknick
請參閱我的編輯代碼 – vikingosegundo
請注意,Viking的解決方案僅適用於Mac OS。 iOS不支持UIKit對象中的混合文本樣式。您必須使用Web視圖或下拉到Core Text以在同一個字符串中顯示多個樣式。您也可以通過將單獨的標籤放在一起來僞造它。 –
http://stackoverflow.com/questions/3952571/uilabel-with-two-different-color-text的可能重複 – Adam