iOS13 UISegmentedControl關(guān)于setTitleTextAttributes的實現(xiàn)似乎發(fā)生了變化

iOS13和以前的版本相比,UISegmentedControlsetTitleTextAttributes函數(shù)的內(nèi)部實現(xiàn)方式發(fā)生了變化。

以代碼為例:

- (void)setNeedNavigationBarWithSegment: (NSArray<NSString *> *)titleArray {
    ...
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:titleArray];
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[Color standardBlue]} forState:UIControlStateNormal];
    ...
}

我們在基類控制器中自行繪制頭部視圖,上述函數(shù)是一個快速構(gòu)造頭部帶有UISegmentedControl的頭部視圖,我們在快速構(gòu)造函數(shù)中配置了segment的選中文本顏色和普通狀態(tài)文本顏色。

然后在一個特殊的控制器中我們需要配置segemnt的字體大小小于我們默認(rèn)的字體大小。

if ([self.titleView isKindOfClass:[UISegmentedControl class]]) {
        UIFont *font = [UIFont systemFontOfSize:12];
        NSMutableDictionary *normalAttributes = [NSMutableDictionary dictionary];        
        normalAttributes[NSFontAttributeName] = font;
        [((UISegmentedControl *)self.titleView) setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
    }

在iOS13以下的版本通過setTitleTextAttributes函數(shù)配置相關(guān)屬性時,上述代碼對字體大小的修改能與更上面對字體顏色的修改共存。

而在iOS13中,setTitleTextAttributes則會覆蓋掉字體顏色,使得Segment會顯示默認(rèn)的黑色文本。

于是就需要

        UIFont *font = [UIFont systemFontOfSize:12];
        NSMutableDictionary *normalAttributes = [NSMutableDictionary dictionary];
        NSMutableDictionary *selectedAttributes = [NSMutableDictionary dictionary];
        
        normalAttributes[NSFontAttributeName] = font;
        normalAttributes[NSForegroundColorAttributeName] = [Color standardBlue];
        selectedAttributes[NSFontAttributeName] = font;
        selectedAttributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [((UISegmentedControl *)self.titleView) setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
        [((UISegmentedControl *)self.titleView) setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];

重新配置所有屬性,或者:

        [((UISegmentedControl *)self.titleView) titleTextAttributesForState:<#(UIControlState)#>]

在配置參數(shù)前預(yù)先獲取已有的參數(shù)字典。

聯(lián)想到iOS13蘋果官方對UISegmentedControl的大動作修改,合理地通過Demo進行聯(lián)想,setTitleTextAttributes的內(nèi)部實現(xiàn)已從原來的合并變成了覆蓋。

以上。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容