【Swift/UIKit】文字の行間や文字同士の間隔を調整する方法!Storyboard

この記事からわかること

  • Swift/UIKit文字行間文字同士間隔調整する方法
  • Storyboard設定するには?
  • NSMutableParagraphStyleNSAttributedStringでコードから設定する方法

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

環境

適応前

【Swift UI】文字の行間や文字同士の間隔を調整する方法!lineSpacing/kerning

UILabelの行間を調整する方法:Storyboard

StoryboardからUILabelの行間を調整するにはインスペクタエリアから「Text」を「Plane」ではなく「Attribute」に変更して位置揃えなどが並んでいるバーの一番右の三角形をタップします。すると以下のようなポップアップが表示されるので「spacing」の値を調整することで行間を調整することができます。

Swift/UIKit】文字の行間や文字同士の間隔を調整する方法!Storyboard

UILabelの行間を調整する方法:コード

コードからUILabelの行間を調整するにはNSMutableParagraphStyleNSAttributedStringを使用します。キーはNSAttributedString.Key.paragraphStyleになります。

override func viewDidLoad() {
    super.viewDidLoad()

    // ラベルのインスタンスを作成
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    label.center = view.center
    // 表示可能な行数を指定
    label.numberOfLines = 100
    // 行間を設定するためのスタイルを作成
    let paragraphStyle = NSMutableParagraphStyle()
    // 行間の値を指定
    paragraphStyle.lineSpacing = 10  
    
    let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n Excepteur sint occaecat cupidatat non proident,\n sunt in culpa qui officia deserunt mollit anim id est laborum."
    
    // スタイルを含む属性文字列を作成
    let attributedText = NSAttributedString(string: text, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])

    // ラベルに属性文字列を設定
    label.attributedText = attributedText
    view.addSubview(label)
}
【Swift UI】文字の行間や文字同士の間隔を調整する方法!lineSpacing/kerning

UILabelの文字の間隔を調整する方法:コード

コードからUILabelの文字の間隔を調整するには先ほど同様にNSAttributedStringを使用します。キーはNSAttributedString.Key.kernになります。

// ラベルのインスタンスを作成
let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
label.center = view.center
label.numberOfLines = 100

// 文字の間隔を指定
let letterSpacing: CGFloat = 10.0

let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n Excepteur sint occaecat cupidatat non proident,\n sunt in culpa qui officia deserunt mollit anim id est laborum."

// スタイルを含む属性文字列を作成
let attributedText = NSAttributedString(string: text, attributes: [NSAttributedString.Key.kern: letterSpacing])

// ラベルに属性文字列を設定
label.attributedText = attributedText
view.addSubview(label)
【Swift UI】文字の行間や文字同士の間隔を調整する方法!lineSpacing/kerning

「カーニング」とはソフトウェアなどでの隣通りの文字同士間隔を調整する機能のことを指します。

Swift UIはこちら

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

New Article

index