很多時(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è)置的字體大小是沒有效果的。這個是大家需要注意的地方,也是我寫本文的目的。