ios – 如何使用Swift设置所选段的文本色调?

我使用以下代码来设置UISegmentedControl的背景.
homeSegment.setDividerImage(UIImage(named: "seperator.png"),forLeftSegmentState: UIControlState.normal,rightSegmentState: UIControlState.normal,barMetrics: UIBarMetrics.Default)



homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"),forState: UIControlState.Selected,barMetrics: UIBarMetrics.Default)
homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"),forState: UIControlState.normal,barMetrics: UIBarMetrics.Default)

如何设置所选分段的文本颜色

解决方法

我通过使用setTitleTextAttributes来实现这一目标.下面的示例使用字典来设置属性,因为您最有可能想要同时设置字体类型和大小.尝试将此选定的段文本颜色设置为红色:
let segAttributes: NSDictionary = [
       NSForegroundColorAttributeName: UIColor.redColor(),NSFontAttributeName: UIFont(name: "Avenir-MediumOblique",size: 20)!
]

homeSegment.setTitleTextAttributes(segAttributes as [NSObject : AnyObject],forState: UIControlState.Selected)

例:

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...