このようにすることで”⼤”のみ⼤きくできるが、var body: some View などの@ViewBuilder内に書く とエラーがでてしまう。 let text = "⼤⼩" let attributedString = AttributedString(text) if let range = attributedString.range(of: "⼤") { attributedString[range].font = .boldSystemFont(ofSize: 30) } KeyPathを⽤いてStringのメソッドで使えるように
Value>, value: Value ) -> AttributedString { var attributedString = AttributedString(self) if let range = attributedString.range(of: range) { attributedString[range][keyPath: keyPath] = value } return attributedString } } 引数にAttributedSubstringへのKeyPathを持つことで、AttriutedSubstringのプロパティを指定できる。 valueの型はKeyPathで指定したpropertyの型に応じて変化する。
AttributedString(text) if let range = attributedString.range(of: "⼤") { attributedString[range].font = .boldSystemFont(ofSize: 30) } • before ViewBuilder内にかけるようになった