【问题标题】:UITextView Color of the selected textUITextView 所选文本的颜色
【发布时间】:2015-03-24 00:27:51
【问题描述】:

如果我有 UITextViewtextview.selectable = true,并且我想使用 UIButton 更改选定的 TextColor,我该如何使用 swift 执行此操作?

【问题讨论】:

  • 您找到解决方法了吗?如果是,请发布正确答案。

标签: swift uitextview selectedtext


【解决方案1】:

如果您只想更改字符串的选定范围,则必须更改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()
}

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2016-02-02
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多