在项目中,我们狠容易遇到就是一些产品,他们什么也不懂,还就是喜欢瞎搞,比如说苹果原生的UISegmentrol ,产品要求我们怎么怎么做,没办法,谁让人家是产品呢,
于是我就只能看这个UISegmentrol的官方文档了,看一遍不知道怎么修改,从头到尾细致地看了两遍,终于知道怎么弄了,
要的是这样的一个效果,通过看官方的API,找到了答案,文档上是这么说的:
/*
You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
*/
- (void)setTitleTextAttributes:(nullable NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
看到上边这几句话,我顿时眼前一亮,马上code就出来了,效果也就自然而然形成,code如下:
//修改系统的UISegmentControl
UIColor *textColor = PB_COLOR_18;//black
NSDictionary *textColorDic = [NSDictionary dictionaryWithObject:textColor forKey:NSForegroundColorAttributeName];
[segmentControl setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
UIColor *selectTextColor = PB_COLOR_1;//blue
NSDictionary *selectTextColorDic = [NSDictionary dictionaryWithObject:selectTextColor forKey:NSForegroundColorAttributeName];
[segmentControl setTitleTextAttributes:selectTextColorDic forState:UIControlStateSelected];
OK。解决
网友评论