【问题标题】:Remove tint color but keep font color UISegmentedControl删除色调但保留字体颜色 UISegmentedControl
【发布时间】:2018-03-01 01:02:35
【问题描述】:

我想从我的UISegmetedControl 中删除选定的颜色。我知道tintColor 可以做到这一点,但这也会删除字体颜色。也使用kCTForegroundColorAttributeName 将删除两者。

旁注我做了一个UIView 并将其放在选定段的上方以显示选定状态。我认为这会更好看。尝试扩展并制作我自己的自定义控件。

public let topLine = UIView()

override func awakeFromNib() {
    super.awakeFromNib()
    self.removeBorders()
    setFont()
    addTopLine()
}

func setFont() {
    let font = UIFont(name: FontTypes.avenirNextUltraLight, size: 22.0)!
    let textColor = UIColor.MyColors.flatWhite
    let attribute = [kCTFontAttributeName:font]
    self.setTitleTextAttributes(attribute, for: .normal)
}

func addTopLine() {
    topLine.backgroundColor = UIColor.MyColors.flatWhite
    let frame = CGRect(x: 7,
                       y: -5,
                       width: Int(self.frame.size.width)/2,
                       height: 2)
    topLine.frame = frame
    self.addSubview(topLine)
}

struct FontTypes {
     static let avenirNextRegular = "AvenirNext-Regular"
     static let avenirLight = "Avenir-Light"
     static let avenirNextUltraLight = "AvenirNext-UltraLight"
}

【问题讨论】:

    标签: uisegmentedcontrol uicolor ios11 uifont swift4


    【解决方案1】:

    TintColor 是用

    附加的
    • 选中段的背景色,

    • 未选中段的文本颜色和

    • UISegmentedControl 的边框颜色。

    因此,如果您要将 tintColor 更改为白色,那么背景颜色和色调颜色都将消失。

    您需要设置 Selected/Unselected 文本属性,如下所示:

    mySegment.tintColor = .white
    
    let selectedAtrribute = [NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]
    mySegment.setTitleTextAttributes(selectedAtrribute as [NSObject : AnyObject], for: UIControlState.selected)
    
    let unselected = [NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]
    mySegment.setTitleTextAttributes(unselected as [NSObject : AnyObject], for: UIControlState.normal)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 2020-01-11
      • 2019-12-18
      相关资源
      最近更新 更多