iOS基本控件之UISegmentrol

在项目中,我们狠容易遇到就是一些产品,他们什么也不懂,还就是喜欢瞎搞,比如说苹果原生的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。解决

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容