UITextView 所选文本的颜色
UITextView Color of the selected text
如果我有 UITextView
和 textview.selectable = true
,并且我想使用 UIButton
更改选定的 TextColor,我如何使用 swift 执行此操作?
如果你只是想改变字符串的选择范围,你必须改变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()
}
如果我有 UITextView
和 textview.selectable = true
,并且我想使用 UIButton
更改选定的 TextColor,我如何使用 swift 执行此操作?
如果你只是想改变字符串的选择范围,你必须改变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()
}