[SwiftUI]TextEditorの背景色を変更するには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

TextEditorの背景色を変更する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

TextEditorの背景色を変更するには、UITextViewとbackground修飾子を使います。

まず、initメソッドやonAppear修飾子で、UITextView.appearance().backgroundColorに「.clear」を代入します。

init() {
     UITextView.appearance().backgroundColor = .clear
}

そして、TextEditorにbackground修飾子を付与します。

backgroud修飾子の引数には、背景色を指定します。

TextEditor(text: $editText)
    .background(背景色)

background修飾子の引数に指定した色が背景色になります。

使用例

struct ContentView: View {
    @State var editText = ""
    
    init() {
        UITextView.appearance().backgroundColor = .clear
    }
    var body: some View {
        VStack {
            TextEditor(text: $editText)
                .frame(width: 200, height: 100)
                .border(Color.black, width: 2)
                .background(Color.blue.opacity(0.2))
        }
    }
}

コメント

タイトルとURLをコピーしました