【发布时间】:2015-03-24 00:27:51
【问题描述】:
如果我有 UITextView 和 textview.selectable = true,并且我想使用 UIButton 更改选定的 TextColor,我该如何使用 swift 执行此操作?
【问题讨论】:
-
您找到解决方法了吗?如果是,请发布正确答案。
标签: swift uitextview selectedtext
如果我有 UITextView 和 textview.selectable = true,并且我想使用 UIButton 更改选定的 TextColor,我该如何使用 swift 执行此操作?
【问题讨论】:
标签: swift uitextview selectedtext
如果您只想更改字符串的选定范围,则必须更改attributedText 属性。您可以执行以下操作:
@IBAction func didTapButton(sender: UIButton) {
let range = textView.selectedRange
let string = NSMutableAttributedString(attributedString: textView.attributedText)
let attributes = [NSForegroundColorAttributeName: UIColor.redColor()]
string.addAttributes(attributes, range: textView.selectedRange)
textView.attributedText = string
textView.selectedRange = range
}
如果要更改整个字符串,可以使用 CeceXX 建议的技术。
@IBAction func didTapButton(sender: UIButton) {
textView.textColor = UIColor.redColor()
}
【讨论】: