33
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでUITextViewの一部の文字色を変更する方法

Last updated at Posted at 2015-02-23

NSMutableAttributedStringを使用すれば、簡単に文字列の一部の装飾を変更することができます。

let attrText = NSMutableAttributedString(string: "rich text")
attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 4))
textView.attributedText = attrText

sample.png

上記は文字色を変更する方法ですが、他にも文字の背景色 (NSBackgroundColorAttributeName) 等を変更できます。

また、UILabelやUITextFieldでも同様に文字の装飾をすることができます。

33
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?