問題描述
給UILabel設(shè)置了highlightedTextColor
屬性,在執(zhí)行label.highlighted = YES
后,label并不顯示highlightedTextColor
對應(yīng)的顏色。
發(fā)現(xiàn)問題
翻閱代碼,發(fā)現(xiàn)此label并非用text
來賦值顯示的文本,而是使用attributedText
。經(jīng)過嘗試,發(fā)現(xiàn)導(dǎo)致問題的原因是初始化attributedText
中NSForegroundColorAttributeName
對應(yīng)的UIColor
跟此label的textColor
不一致,當(dāng)兩者一樣的時,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.
當(dāng)給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
,設(shè)置textColor
為[UIColor blueColor]
,設(shè)置highlightedTextColor
為[UIColor redColor]
,然后給attributedText
賦值,并設(shè)置highlighted
為YES
,運行之后,結(jié)果界面如下:
可以看到,attributedText
中顏色值為[UIColor blueColor]
的字段,顏色變成了highlightedTextColor
的顏色,即顏色跟textColor
一樣的字段會響應(yīng)highlighted
屬性的變化。
attributedText
被賦了新值,此時,label的textColor
會收到影響,值會跟attributedText
開頭字段(此處為Hello,Wolrd!
)的樣式統(tǒng)一,即[UIColor grayColor]
。 -
那此時,再給
attributedText
賦值時,是否是顏色值為[UIColor grayColor]
的字段會響應(yīng)highlighted
屬性,點擊按鈕,執(zhí)行appendString
來驗證一下,結(jié)果界面如下:
跟設(shè)想的不一樣,依然是顏色為[UIColor blueColor]
的字段響應(yīng)了highlighted
屬性,不過驗證了textColor
確實變成了[UIColor grayColor]
。
此處猜想,在設(shè)置textColor
時,UILabel
內(nèi)部有個私有屬性保存了這個值,當(dāng)設(shè)置attributedText
時,只會改變textColor
,不會改變這個私有屬性,這個私有屬性來判斷attributedText
中哪些字段需要響應(yīng)highlighted
。 -
改變
highlightedTextColor
,又會有什么影響,點擊按鈕,運行changeHighlightedTextColor
,效果界面如下:
由此可見,只是改變了highlightedTextColor
,沒有其他事先沒有預(yù)想到的問題。 改變
textColor
,從文檔可以看出,將會改變attributedText
中所有字段的顏色顯示,也可以預(yù)料,改變之后,attributedText
中的所有字段都會響應(yīng)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
,效果界面如下:
運行結(jié)果跟設(shè)想的一樣。
總結(jié)
到此,本次對attributedText
的探索就結(jié)束了,font
、textColor
、attributedText
都會互相影響,當(dāng)只是給文本添加段落格式,或者實現(xiàn)統(tǒng)一字體顏色的文本和圖片的混合顯示,這種情況下,不建議通過NSFontAttributeName
和NSForegroundColorAttributeName
來定義字體大小和顏色顯示,應(yīng)該直接設(shè)置font
和textColor
,個人見解,歡迎討論。