【问题标题】:How to Set tint color of text only of selected segment using Swift?如何使用 Swift 仅设置选定段的文本的色调颜色?
【发布时间】:2015-04-14 08:59:39
【问题描述】:

我使用以下代码设置 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)

如何设置选中段的文字颜色?

【问题讨论】:

    标签: ios swift uisegmentedcontrol


    【解决方案1】:

    我通过使用setTitleTextAttributes 来实现这一点。下面的示例使用字典来设置属性,因为您很可能希望同时设置字体类型和大小。试试这个将您选择的段文本颜色设置为红色:

    let segAttributes: NSDictionary = [
           NSForegroundColorAttributeName: UIColor.redColor(), 
                      NSFontAttributeName: UIFont(name: "Avenir-MediumOblique", size: 20)!
    ]
    
    homeSegment.setTitleTextAttributes(segAttributes as [NSObject : AnyObject], forState: UIControlState.Selected)
    

    例子:

    【讨论】:

    • 如何为每个段设置不同的颜色?假设我希望第一个在突出显示时为红色,但第二个为黑色。我将如何实现这一目标?
    • @JoseTapizquent - 这是较旧的代码,但请查看 my answer 到另一个问题。它解决了选定的状态色调。
    【解决方案2】:

    @Portland Runner Answer 适用于旧版本的 Swift。

    Swift 3.0,这行得通。

    逻辑与@Portland Runner 相同,但语法根据 Swift 3.0 进行了修改和更新

    这里我实现的这段代码是Segment控件的扩展,可用于应用程序中的所有Segment控件,其中代码集必须在应用程序类中定义。

    extension UISegmentedControl {
    func setSegmentStyle() {
        setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default)
        setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
        setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
    
    
    
         let segAttributes: NSDictionary = [
                NSForegroundColorAttributeName: UIColor.gray,
                NSFontAttributeName: UIFont(name: "System-System", size: 14)!
            ]
    
            setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.selected)
        }
    
        // create a 1x1 image with this color
        private func imageWithColor(color: UIColor) -> UIImage {
            let rect = CGRect(x: 0.0, y: 0.0, width:  1.0, height: 1.0)
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            context!.setFillColor(color.cgColor);
            context!.fill(rect);
            let image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return image!
        }
    }
    

    以下代码在任何地方都可以使用

     self.mySegment.setSegmentStyle()
    

    【讨论】:

      【解决方案3】:

      对于 Swift 4.0

      let fontAttribute = [NSAttributedStringKey.font: UIFont(name: "Montserrat-Regular", size: 12)!, 
                             NSAttributedStringKey.foregroundColor: UIColor.red]
      
      segmentedControl.setTitleTextAttributes(fontAttribute, for: .normal)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 2021-08-25
        • 2014-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多