修改UISegmentedControl文本的字體和顏色時(shí)的注意點(diǎn)

很多時(shí)候我們會使用UISegmentedControl來做菜單切換,根據(jù)自己的需求難免需要定制文本的字體大小和選中前后的字體顏色,其實(shí)修改字體大小和更改顏色的實(shí)現(xiàn)方式很簡單,網(wǎng)上一搜一大把,比如下面是我常用的方式:

//初始化

segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"標(biāo)題1",@"標(biāo)題2"]];

segmentedControl.frame = CGRectMake(0, 0, 180, 30);

segmentedControl.clipsToBounds = YES;

segmentedControl.layer.cornerRadius = 12;

segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;

segmentedControl.layer.borderWidth = 1;

segmentedControl.backgroundColor = [UIColor clearColor];

segmentedControl.tintColor = [UIColor whiteColor];

segmentedControl.selectedSegmentIndex = 0;

[segmentedControl addTarget:self action:@selector(onSegmentValueChanged:) forControlEvents:UIControlEventValueChanged];

1.修個字體大小

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0f],NSFontAttributeName ,nil];

[segmentedControl setTitleTextAttributes:dic forState:UIControlStateNormal];

[segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];

2.修改字體顏色(kWhiteColor-白色,kTopBarBgColor-導(dǎo)航欄顏色)

[segmentedControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:kWhiteColor forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];

[segmentedControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:kTopBarBgColor forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];

上面就是設(shè)置字體大小和顏色的代碼,當(dāng)然網(wǎng)上可能有其他寫法,但是本質(zhì)是一樣的。我這樣要說的不僅僅是這些,而是在設(shè)置這個的時(shí)候需要注意的地方。也行有人會說這個注意什么啊,只要代碼寫了就OK了啊,不過我想也有人遇到過設(shè)置了以后沒有效果的經(jīng)歷吧,如果你設(shè)置了就OK了,那么說明你設(shè)置的順序?qū)α?,巧了,就避過了問題所在。這里的問題就是設(shè)置的時(shí)候要先設(shè)置顏色后設(shè)置字體,否則設(shè)置的字體大小是沒有效果的。這個是大家需要注意的地方,也是我寫本文的目的。

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

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

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