SwiftUI Multiline Text Background Color
It seems SwiftUI doesn't support that yet (Xcode 11.3). You can create a UIViewRepresentable
for a UILabel
with an attributedString
though. Or use AttributedText from SwiftUIX.
Change background color of TextEditor in SwiftUI
iOS 16
You should hide the default background to see your desired one:
TextEditor(text: .constant("Placeholder"))
.scrollContentBackground(.hidden) // <- Hide it
.background(.red) // To see this
iOS 15 and below
TextEditor
is backed by UITextView
. So you need to get rid of the UITextView
's backgroundColor
first and then you can set any View
to the background
.
struct ContentView: View {
init() {
UITextView.appearance().backgroundColor = .clear
}
var body: some View {
List {
TextEditor(text: .constant("Placeholder"))
.background(.red)
}
}
}
Demo
You can find my simple trick for growing TextEditor here in this answer
SwiftUI multiline text always truncating at 1 line
Please see this answer for Xcode 11 GM:
https://stackoverflow.com/a/56604599/30602
Summary: add .fixedSize(horizontal: false, vertical: true)
— this worked for me in my use case.
How do I create a multiline Textfiled in SwiftUI?
Here is a 90% solution. It deletes further text after the 3rd line break, but it cannot detect line wraps in long texts.
struct ContentView: View {
@State private var notes = ""
var body: some View {
VStack (alignment: .leading) {
Text("Notes")
.fontWeight(.bold)
CustomTextEditor("Enter your note", text: $notes)
}
.padding()
}
}
struct CustomTextEditor: View {
init(_ prompt: LocalizedStringKey, text: Binding<String>) {
self.prompt = prompt
self._text = Binding(projectedValue: text)
}
let prompt: LocalizedStringKey
@Binding var text: String
var body: some View {
ZStack(alignment: .topLeading ) {
Text(prompt)
.foregroundColor(text == "" ? .secondary : .clear)
.padding(EdgeInsets(top: 7, leading: 3, bottom: 0, trailing: 0))
TextEditor(text: $text)
// deleting everything after the 3rd linebreak
.onChange(of: text) { _ in
let stringArray = text.map { $0 }
let pos = stringArray.indices.filter { stringArray[$0] == "\n"}
if pos.count > 2 {
text = String(stringArray.prefix(upTo: pos[2]))
}
}
}
.frame(height: 82)
.padding(10)
.background(Color(.systemGray5))
.cornerRadius(20)
// getting rid of TextEditor standard background
.onAppear {
UITextView.appearance().backgroundColor = .clear
}
.onDisappear {
UITextView.appearance().backgroundColor = .systemGray5
}
}
}
MacOs SwiftUI textfield focus effect color change
It is possible to use background with focused dependent state, like
.background(RoundedRectangle(cornerRadius: 4)
.stroke(Color.gray, lineWidth: 0.5).cornerRadius(4)
.background(focused ? .black : .clear) // << here !!
.clipShape(RoundedRectangle(cornerRadius: 4))
)
Complete test module is here
swiftUI: possible to have background color ignore safe zone and content adhere to safe zone?
You only want the Color
to extend into the safe area, so only put ignoresSafeArea
on the Color
. Also, if you're using iOS 14+, you should use ignoresSafeArea(_:edges:)
instead of the deprecated edgesIgnoringSafeArea(_:)
as West1 said.
VStack {
Text("Foo")
}
.background(
Color.red.ignoresSafeArea()
)
Full example:
struct ContentView: View {
var body: some View {
VStack {
HStack {
Text("Foo")
Spacer()
}
Spacer()
}
.background(
Color.red.ignoresSafeArea() /// `edgesIgnoringSafeArea` is deprecated
)
}
}
Result:
How can I change the text color of selected list items in SwiftUI?
There might be variants but in general you have to pass somehow selection into view with row text and apply foreground color conditionally, like
NavigationLink(
destination: ItemDetail(item: item)
) {
ItemRow(item: item, selected: item == selectedItem)
}
and in ItemRow
Text(item.title)
.foregroundColor(selected ? .blue : .labelColor)
Note: instead of .blue
you can set your custom color created in color assets with light/dark mode variants, so selection be differently colored in those modes.
Related Topics
Saving Coredata to a Web Server with Swift 3.0
Solving System of Equations in Swift
Bringing iOS Frameworks Through Carthage in Xcode 12.0
Error "No Such Module" When Installed Framework with Pod in Swift 3
Swift:Pause and Resume Nstimer
Swift: How to Flush Stdout After Println
Color Keying Video with Gpuimage on a Scnplane in Arkit
Loading Multiple Google Interstitial Ads Makes App Crash
Appdelegate Segue Alternative Pass Data
Best Way to Structure My Firebase Database
Convert Optional String to Int in Swift
Skspritenode Gets Hidden Below Parent Node