2
1

More than 3 years have passed since last update.

UISegmentedControlのフォント色を変える

Posted at

UISegmentedControlの文字色を変える際のメモです。
選択された状態の背景色の変更の記事は良く見かけたのですが、文字色を変える記事はあまりありませんでしたので、書きました。
iOS13以降で確認していますが、基本的にiOS11,12でも同じかと思います。

実装


segment.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .selected) //選択時の文字色
segment.setTitleTextAttributes([.foregroundColor: UIColor.gray], for: .normal)  //非選択時の文字色

UISegmentedControl.setTitleTextAttributes を使用して、選択状態によって色を変えて設定しました。
上記の例では、選択時には赤、日選択時にはグレーになります。

適用結果は以下のようになります。

スクリーンショット 2020-09-01 12.19.58.png

参考

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