LoginSignup
1
1

More than 3 years have passed since last update.

titleTextAttributes指定時のフォント指定忘れ対策

Last updated at Posted at 2020-10-16

概要

iOS のナビゲーションバーの文字色を変更するためにtitleTextAttributesを指定してみたら、フォントが崩れたので対応。

環境

Xcode 11.3
iOS 14.0 & 13.3

事象

ナビゲーションバータイトルにフォント指定している場合、
色だけ指定しても、フォント指定が外れる

  • フォントを(xibで)ヒラギノ角ゴシックにしている場合
    スクリーンショット 2020-10-16 16.30.05.png

  • 特定の画面でタイトルの色変えたいと思って、よくあるように色だけ指定するとフォント指定が外れる(システムデフォルトになっている?)

    self.navigationController?.navigationBar.titleTextAttributes = [
      // 文字の色
       .foregroundColor: UIColor.yellow
    ]


スクリーンショット 2020-10-16 16.29.42.png

対策

  • フォントの指定も加える必要がある
    self.navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.yellow,
        NSAttributedString.Key.font: UIFont(name: "HiraginoSans-W3", size: 16) as Any
    ]


スクリーンショット 2020-10-16 16.36.08.png

当たり前っちゃ当たり前ですが、気づかなかったので、備忘録として投稿します。

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