まず、サンプルとして以下のビューを用意した。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-34.png?resize=856%2C234&ssl=1)
このTextビューに対し、以下のような装飾を付ける様々なモディファイアを今回は紹介していく。
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-35.png?resize=840%2C276&ssl=1)
コンテンツ
フォントの太さを変更する
fontWeightモディファイアでは、引数の値に応じてフォントの太さを変更することができる。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.fontWeight(.heavy)
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-36.png?resize=834%2C226&ssl=1)
ここではheavyを指定しているが、他にも以下の値を設定することができる。
- ultraLight
- thin
- light
- regular
- medium
- semibold
- bold
- heavy
- black
文字のカーニングを設定する
kerningモディファイアでは、カーニング(字間調整)を設定することができる。
引数に正の数を指定すると字間を広げ、負の数を指定すると狭くする。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.kerning(10)
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-37.png?resize=848%2C210&ssl=1)
文字に下線をつける
underlineモディファイアでは、文字に下線を設定することができる。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.underline()
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-39.png?resize=816%2C216&ssl=1)
また、第一引数に下線をつけるかどうかのBool値、第二引数に下線の色を設定することもできる。
.underline(true, color: .red)
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-38.png?resize=828%2C180&ssl=1)
文字に取り消し線をつける
strikethroughモディファイアでは、文字に取り消し線をつけることができる。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.strikethrough()
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-40.png?resize=842%2C198&ssl=1)
こちらもunderlineモディファイア同様、第一引数に取り消し線をつけるかどうかのBool値、第二引数に取り消し線の色を設定することもできる。
行間を設定する
lineSpacingモディファイアでは、テキストの行間を設定することができる。
※行間を確認するためframeで横幅サイズを制限している。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.frame(width: 80)
.lineSpacing(30)
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-41.png?resize=806%2C248&ssl=1)
モディファイアを組み合わせる
これらの装飾モディファイアを組み合わせると、以下のような見た目をしたテキストを作ることができる。
struct ContentView: View {
var body: some View {
Text("Hello World")
.font(.title)
.fontWeight(.heavy)
.kerning(10)
.underline()
.strikethrough()
.frame(width: 150)
.lineSpacing(30)
}
}
![](https://i0.wp.com/chusotsu-program.com/wp-content/uploads/2021/04/image-35.png?resize=840%2C276&ssl=1)