問題描述
給UILabel設置了highlightedTextColor
屬性,在執行label.highlighted = YES
后,label并不顯示highlightedTextColor
對應的顏色。
發現問題
翻閱代碼,發現此label并非用text
來賦值顯示的文本,而是使用attributedText
。經過嘗試,發現導致問題的原因是初始化attributedText
中NSForegroundColorAttributeName
對應的UIColor
跟此label的textColor
不一致,當兩者一樣的時,highlightedTextColor
才有作用。
挖掘問題
查找文檔,UILabel
的attributedText
屬性,有如下解釋
Discussion
This property is
nil
by default.Assigning a new value to this property also replaces the value of the
text
property with the same string data, although without any formatting information. In addition, assigning a new a value updates the values in thefont
,textColor
, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.
當給attributedText
屬性賦值的時候,會影響到text
、font
、textColor
等屬性,本文主要對textColor
做一些深入的了解。
寫些代碼來驗證一下
代碼如下:
- (void)attributedStringOfInputUI {
UILabel *label = [UILabel new];
label.textColor = [UIColor blueColor];
label.highlightedTextColor = [UIColor redColor];
label.attributedText = ({
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:@"Hello,World!"
attributes:@{
NSForegroundColorAttributeName : [UIColor grayColor]
}];
[attributedString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"Again!"
attributes:@{
NSForegroundColorAttributeName : [UIColor blueColor]
}]];
[attributedString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"Again!"
attributes:@{
NSForegroundColorAttributeName : [UIColor blueColor]
}]];
attributedString;
});
label.highlighted = YES;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
}];
self.label = label;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"添加文本" forState:UIControlStateNormal];
[button addTarget:self action:@selector(appendString) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view).offset(-90);
}];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
[button2 setTitle:@"改變highlightedTextColor" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(changeHighlightedTextColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
[button2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view).offset(-60);
}];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
[button3 setTitle:@"改變TextColor" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(changeTextColor) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
[button3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view).offset(-30);
}];
}
- (void)appendString {
NSMutableAttributedString *attributedString = self.label.attributedText.mutableCopy;
[attributedString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"Again!"
attributes:@{
NSForegroundColorAttributeName : self.label.textColor
}]];
[attributedString appendAttributedString:[[NSAttributedString alloc]
initWithString:@"Again!"
attributes:@{
NSForegroundColorAttributeName : [UIColor blueColor]
}]];
self.label.attributedText = attributedString;
}
- (void)changeHighlightedTextColor {
self.label.highlightedTextColor = [UIColor greenColor];
}
- (void)changeTextColor {
self.label.textColor = [UIColor grayColor];
self.label.highlighted = !self.label.highlighted;
}
-
初始化
UILabel
,設置textColor
為[UIColor blueColor]
,設置highlightedTextColor
為[UIColor redColor]
,然后給attributedText
賦值,并設置highlighted
為YES
,運行之后,結果界面如下:
可以看到,attributedText
中顏色值為[UIColor blueColor]
的字段,顏色變成了highlightedTextColor
的顏色,即顏色跟textColor
一樣的字段會響應highlighted
屬性的變化。
attributedText
被賦了新值,此時,label的textColor
會收到影響,值會跟attributedText
開頭字段(此處為Hello,Wolrd!
)的樣式統一,即[UIColor grayColor]
。 -
那此時,再給
attributedText
賦值時,是否是顏色值為[UIColor grayColor]
的字段會響應highlighted
屬性,點擊按鈕,執行appendString
來驗證一下,結果界面如下:
跟設想的不一樣,依然是顏色為[UIColor blueColor]
的字段響應了highlighted
屬性,不過驗證了textColor
確實變成了[UIColor grayColor]
。
此處猜想,在設置textColor
時,UILabel
內部有個私有屬性保存了這個值,當設置attributedText
時,只會改變textColor
,不會改變這個私有屬性,這個私有屬性來判斷attributedText
中哪些字段需要響應highlighted
。 -
改變
highlightedTextColor
,又會有什么影響,點擊按鈕,運行changeHighlightedTextColor
,效果界面如下:
由此可見,只是改變了highlightedTextColor
,沒有其他事先沒有預想到的問題。 改變
textColor
,從文檔可以看出,將會改變attributedText
中所有字段的顏色顯示,也可以預料,改變之后,attributedText
中的所有字段都會響應highlighted
。
If you are using styled text, assigning a new value to this property causes the color to be applied to the entirety of the string in the
attributedText
property. If you want to apply the color to only a portion of the text, create a new attributed string with the desired style information and associate it with the label. If you are not using styled text, this property applies to the entire text string in thetext
property.
點擊按鈕,第一次運行changeTextColor
,效果界面如下:
點擊按鈕,第二次運行changeTextColor
,效果界面如下:
運行結果跟設想的一樣。
總結
到此,本次對attributedText
的探索就結束了,font
、textColor
、attributedText
都會互相影響,當只是給文本添加段落格式,或者實現統一字體顏色的文本和圖片的混合顯示,這種情況下,不建議通過NSFontAttributeName
和NSForegroundColorAttributeName
來定義字體大小和顏色顯示,應該直接設置font
和textColor
,個人見解,歡迎討論。