swift - 自定义分段控制中每个分段的字体颜色

标签 swift uisegmentedcontrol textcolor

我正在尝试配置分段控制栏。

目标是使每个片段的文本字体具有不同的颜色。所以它应该看起来像这样: example

我读过很多主题,包括这个:How to set UISegmentedControl Tint Color for individual segment 但主要讨论如何使色调颜色不同。我尝试在正常模式下使颜色不同。

最佳答案

一个想法是递归遍历段的 View 层次结构并检查是否遇到 UILabel

照常设置您的 UISegmentControl

func addControl()  {
    let items = ["One", "Two", "Three"]
    let segmentedControl = UISegmentedControl(items: items)
    segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
    segmentedControl.center = view.center
    segmentedControl.selectedSegmentIndex = 1
    view.addSubview(segmentedControl)
    
    // custom function
    updateSegmentTextColor(segmentedControl)
}

此函数遍历 View 层次结构并设置标签的颜色

// Just for testing
func randomColor() -> UIColor
{
    let red = CGFloat(arc4random_uniform(256)) / 255.0
    let blue = CGFloat(arc4random_uniform(256)) / 255.0
    let green = CGFloat(arc4random_uniform(256)) / 255.0
    
    return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

private func updateSegmentTextColor(_ rootView: UIView)
{
    for subview in rootView.subviews
    {
        if let label = subview as? UILabel
        {
            // Any color you want
            label.textColor = randomColor()
        }
        
        updateSegmentTextColor(subview)
    }
}

结果:

Custom Different UISegmentControl Title Text Color

最后一些注意事项:

  • 此解决方案可能会影响您可能想要设置的其他色调属性,并且当您想要为选定状态和取消选定状态保持相同的文本颜色时非常有用
  • future 的 iOS 更新和 Segment Control 的实现可能会对其工作方式产生影响

更新

如果您想指定特定段的标题颜色,您可以假设(赌博)段控件应按逻辑顺序布局其 subview 。

我强调假设,因为这个命令不在我们的控制范围内,你只能受实现的支配。

事实上,如果您设置选定索引segmentedControl.selectedSegmentIndex = 1并且您有4个段,则布局 subview 的顺序为0,2,3,1,因此选定的段得到最后添加。

适合您情况的方法如下:

// Add a color queue to hold the colors for the 4 segments
var colorQueue: [UIColor] = [.red, .blue, .green, .orange]

private func updateSegmentTextColor(_ rootView: UIView)
{
    for subview in rootView.subviews
    {
        if let label = subview as? UILabel,
           !colorQueue.isEmpty
        {
            // Dequeue the front of the queue
            let color = colorQueue.removeFirst()
            label.textColor = color
        }
        
        updateSegmentTextColor(subview)
    }
}

private func addControl()  {
    let items = ["One", "Two", "Three", "Four"]
    let segmentedControl = UISegmentedControl(items: items)
    segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
    segmentedControl.center = view.center
    segmentedControl.tintColor = .blue
    view.addSubview(segmentedControl)
    
    // custom function
    updateSegmentTextColor(segmentedControl)
    
    // Add the selected index if you need AFTER the updates
    segmentedControl.selectedSegmentIndex = 1
}

你明白了

Custom UISegmentControl Title Label Color Swift iOS UIKit

关于swift - 自定义分段控制中每个分段的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71140645/

相关文章:

ios - Swift native 代码返回 iOS 版本而不是 bool

swift - 尝试更改邮件撰写导航栏文本颜色

ios - tvos UISegmentedControl 焦点样式不改变

ios - 带有两种不同颜色文本的 UILabel

android - 如何为 Android 单选按钮使用 setTextColor?

ios - 无法将类型 'JSONObject.Type'(又名 'Dictionary<String, Any>.Type')的值分配给类型 'JSONObject'(又名 'Dictionary<String, Any>')

ios - 如何修复尝试递归调用 -save coredata 错误?

ios - 我如何将 UISegmentedControl 子类化,以便各个段识别 UILongPressGestureRecognizer?

ios - 如何根据 segmentedControl 索引更改数据

java - Android:在 ExpandableListViewActivity 的子级中访问 TextView