UILabel、UITextView中的attributedText

問題描述

給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)致問題的原因是初始化attributedTextNSForegroundColorAttributeName對應(yīng)的UIColor跟此label的textColor不一致,當(dāng)兩者一樣的時,highlightedTextColor才有作用。

挖掘問題

查找文檔,UILabelattributedText屬性,有如下解釋

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 the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.

當(dāng)給attributedText屬性賦值的時候,會影響到textfonttextColor等屬性,本文主要對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è)置highlightedYES,運行之后,結(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 the text property.

點擊按鈕,第一次運行changeTextColor,效果界面如下:

點擊按鈕,第二次運行changeTextColor,效果界面如下:

運行結(jié)果跟設(shè)想的一樣。

總結(jié)

到此,本次對attributedText的探索就結(jié)束了,fonttextColorattributedText都會互相影響,當(dāng)只是給文本添加段落格式,或者實現(xiàn)統(tǒng)一字體顏色的文本和圖片的混合顯示,這種情況下,不建議通過NSFontAttributeNameNSForegroundColorAttributeName來定義字體大小和顏色顯示,應(yīng)該直接設(shè)置fonttextColor,個人見解,歡迎討論。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內(nèi)容