以前设置UISegmentControl字体大小和颜色,代码我是这样写的:
//设置正常状态和选中状态下的字体大小
[goodsSementControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:LNLableFont(16.0f) forKey:NSFontAttributeName] forState:UIControlStateNormal];
[goodsSementControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:LNLableFont(16.0f) forKey:NSFontAttributeName] forState:UIControlStateSelected];
//设置正常状态和选中状态下的背景颜色
[goodsSementControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateNormal];
[goodsSementControl setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName] forState:UIControlStateSelected];
仔细检查下,发现在代码没有什么问题啊,就是达不到自己想要的那种效果。后来发现,设置背景色的代码覆盖了设置字体大小的代码,最后换了种设置方式问题解决:
//使用一个字典同时设置字体大小和背景色在某种状态下
[goodsSementControl setTitleTextAttributes:@{NSFontAttributeName:LNLableFont(16),NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
[goodsSementControl setTitleTextAttributes:@{NSFontAttributeName:LNLableFont(16),NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];