4

UITextViewを使用していてtextview.selectable = true、選択した TextColor を を使用して変更したい場合UIButton、swift を使用してこれを行うにはどうすればよいですか?

4

1 に答える 1

8

文字列の選択範囲を変更するだけの場合は、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()
}
于 2015-01-25T13:29:35.557 に答える