0
0

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 3 years have passed since last update.

Swift NSAttributedString

Posted at

NSAttributedString

storyboardやUILabelのプロパティを操作して装飾をすることができるが、NSAttributedStringを使用することでより細かい装飾をすることができる。

辞書型で指定した文字列の装飾をNSAttibutedStringの引数にいれることによって描画できる

var dictionary:[NSAttributedString.Key: Any] = [
        NSAttributedString.Key.font: NSFont.systemFont(ofSize: 40),
        NSAttributedString.Key.foregroundColor: NSColor.orange,
        NSAttributedString.Key.strokeColor: NSColor.black,
        NSAttributedString.Key.strokeWidth: -3
    ]
//辞書型を引数(attributeds)に入れる。
let attrString = NSAttributedString(string: "Swift", attributes: dic)

storyboardだとどうしてもうまく表現されないことも多々あったり、動的に文字列をうまく変更したい場合はよく使うものかな
それぞれのvalueは色が入ったり、サイズが入ったり、セットで描画されるのもあるので拡張機能として、再利用可能な形にしてから使い回す使用方法が多い。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?