LoginSignup
45
24

More than 5 years have passed since last update.

UITextViewのInsetについて

Last updated at Posted at 2015-08-24

UITextViewの文字周りにマージンを入れたい場合はcontentInsetを使ってもダメで、textContainerInsetを使わないといけない。

どれくらいダメなのか以下を見るとわかる。

contentInset
        textView.contentInset = UIEdgeInsetsMake(100, 100, 100, 100)
        textView.sizeToFit()

Screen Shot 2015-08-24 at 15.32.29.png
は…という感じ。どう見ても上下左右100pxのマージンなど無い。

textContainerInset
        textView.textContainerInset = UIEdgeInsetsMake(100, 100, 100, 100)
        textView.sizeToFit()

Screen Shot 2015-08-24 at 15.32.45.png

というわけでtextContainerInsetを使うのが正解。

じゃあcontentInsetってなんなんだよってなるんだけど、これはUITextViewの中にあるUIScrollViewに対するInset。
ただUITextViewはテキスト量が自身のサイズを超えないとcontentSizeが溢れないため文字量が少ないとcontentInsetも効かなくなる。

45
24
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
45
24