在Xcode中的分段控件中选择的文本状态

时间:2016-01-05 02:11:19

标签: ios xcode swift cocoa-touch uisegmentedcontrol

我正在使用Swift和Xcode开发我的第一个应用程序。

在我的一个视图中,我有一个分段控件,其中包含表情符号作为显示的文本。但是,我有一个问题,即无论何时选择其中一个表情符号,它都会变黑,如下图所示:

segmentedControl

我认为问题与我的分段控件的选定状态的文本属性有关,我尝试修复时将所选状态的文本属性设置为与正常状态的文本属性相同:

override func viewDidAppear(animated: Bool) {
    moodSelector.setTitleTextAttributes(moodSelector.titleTextAttributesForState(.Normal), forState: .Selected)
}

但是,这似乎不起作用。任何帮助将不胜感激?

1 个答案:

答案 0 :(得分:6)

您可以将NSAttributedStringKey.foregroundColor(以前称为NSForegroundColorAttributeName)设置为任何颜色,它可以阻止该行为。例如,在Swift 3或4中:

override func viewDidLoad() {
    super.viewDidLoad()         // whether `viewDidLoad` or `viewDidAppear`, make sure to call the appropriate `super` method, too

    let attributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
    moodSelector.setTitleTextAttributes(attributes, for: .selected)
}

产量:

segmented control with emojis