2017-09-20 15 views
1

UISegmetedControlから選択した色を削除します。私はtintColorがこれを行うことができますが、それもそれでフォントの色を削除します知っています。また、kCTForegroundColorAttributeNameを使用すると、両方が削除されます。色合いを削除しますが、フォントの色を維持するUISegmentedControl

サイドノート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" 
} 

答えて

2

TintColorは、選択されたセグメントの

  • 背景色、選択されていないセグメントの

  • テキストの色とUISegmentedControlの

  • ボーダー色で取り付けています。

したがって、tintColorを白に変更すると、背景色と色合いの色が両方とも消えてしまいます。

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) 
:あなたは以下のように選択/非選択テキスト属性を設定する必要が

関連する問題