LoginSignup
2
3

More than 5 years have passed since last update.

iOS11とiOS10でUILabelのAttributedの扱いがちょっと違う

Posted at

環境

Swift 4
Xcode 9.2

本題

XibとかStoryboardでUILabelのTextをAttributedに設定した時に、コードで再度文字列を代入した時の挙動に関して、iOS10とiOS11で異なったのでメモ。

XibとかStoryboardでUILabelのTextをAttributedにしてから、LineHeightとかをいじって、以下のようにコードで文字列を代入するとiOS11だとAttributedがそのままだけど、iOS10では消えちゃう。


@IBOutlet private weak var label: UILabel!

self.label.text = "ほげほげ"

なので、iOS10でも動作させる場合は以下のようにNSAttributedStringを作り直して代入する必要がある

var range = NSMakeRange(0, self.label.attributedText?.string.count)
let attrs = self.label.attributedText?.attributes(at: 0, effectiveRange: &range)
self.label.attributedText = NSAttributedString(string: "ほげほげ", attributes: attrs)
2
3
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
2
3