Xcode 分段控件中文本的选定状态
Selected state of text in a segmented control in Xcode
我正在使用 Swift 和 Xcode 编写我的第一个应用程序。
在我的一个观点中,我有一个分段控件,它以表情符号作为显示的文本。 However, I have a problem where whenever one of the emoticons is selected, it becomes blacked out as shown in the image below:
我认为问题与我的分段控件的选定状态的文本属性有关,我尝试修复的方法是将选定状态的文本属性设置为与正常状态:
override func viewDidAppear(animated: Bool) {
moodSelector.setTitleTextAttributes(moodSelector.titleTextAttributesForState(.Normal), forState: .Selected)
}
然而,这似乎不起作用。任何帮助将不胜感激?
您可以将 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)
}
产量:
我正在使用 Swift 和 Xcode 编写我的第一个应用程序。
在我的一个观点中,我有一个分段控件,它以表情符号作为显示的文本。 However, I have a problem where whenever one of the emoticons is selected, it becomes blacked out as shown in the image below:
我认为问题与我的分段控件的选定状态的文本属性有关,我尝试修复的方法是将选定状态的文本属性设置为与正常状态:
override func viewDidAppear(animated: Bool) {
moodSelector.setTitleTextAttributes(moodSelector.titleTextAttributesForState(.Normal), forState: .Selected)
}
然而,这似乎不起作用。任何帮助将不胜感激?
您可以将 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)
}
产量: