読者です 読者をやめる 読者になる 読者になる

のほほん停留所

つぶやきのなりそこないの溜まり場

UILabelの改行まとめ

UILabelの改行の種類をいつも調べてる気がするので備忘録代わりにまとめた

let label = UILabel(frame: .zero)
label.lineBreakMode = .byWordWrapping

lineBreakModeの型であるNSLineBreakModeのドキュメントを見ると、改行方法は6種類ある

NSLineBreakMode - UIKit | Apple Developer Documentation

byWordWrapping

Wrapping occurs at word boundaries, unless the word itself doesn’t fit on a single line. See Characters and Grapheme Clusters in String Programming Guide for a discussion of issues related to determining word boundaries.

f:id:Nonchalanttan:20170317195406p:plain

単語の境界で折り返す

byCharWrapping

Wrapping occurs before the first character that doesn’t fit.

f:id:Nonchalanttan:20170317200447p:plain

文字で折り返す

byClipping

Lines are simply not drawn past the edge of the text container.

f:id:Nonchalanttan:20170317200325p:plain

折り返さず、端を超えた文字は表示されない

byTruncatingHead

The line is displayed so that the end fits in the container and the missing text at the beginning of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.

f:id:Nonchalanttan:20170317200802p:plain

文字列末尾が表示され、行の先頭に三点リーダ

byTruncatingTail

The line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text.

f:id:Nonchalanttan:20170317200954p:plain

文字列先頭が表示され、行の末尾に三点リーダ

byTruncatingMiddle

The line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph. This mode is used for single-line layout; using it with multiline text truncates the text into a single line.

f:id:Nonchalanttan:20170317201055p:plain

文字列先頭と末尾が表示され、中央に三点リーダ