There is no built-in font-weight modifier for textfield in SwiftUI, as of Xcode 11.2.1.
How can we introduce font-weight without extending UITextField as UIViewRepresentable?
A general approach for using standard font size options and weights that work with SwiftUI TextField. For example:
TextField("Name", text: $name)
.font(Font.headline.weight(.light))
Available standard size options (smallest to largest):
.caption
.footnote
.subheadline
.callout
.body
.headline
.title3
.title2
.title
.largeTitle
Available standard font weights (lightest to heaviest):
.ultralight
.thin
.light
.regular
.medium
.semibold
.bold
.heavy
.black
Updated Answer:
Text("Bold Text").bold()
Text("**Bold Text**")
Text("*Italic Text*")
Text("***Bold Italic Text***")
import SwiftUI
struct ContentView: View {
@State var TextValue: String = "Hello"
var body: some View {
VStack {
TextField("placeholder", text: $TextValue)
.padding(.horizontal, 50)
.font(.system(size: 30, weight: .heavy, design: .default))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
SwiftUI supports markdown.
Add double asterisks (**) arroud the text/characters to make it bold.
Text("**This text is bold**")
To emphasize text, use underscore
Text("_This text is italic_")
The updated approach for iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+ is:
.fontWeight(.bold)
Expanding on shawnynicole's answer, you can create an extension:
extension View {
func bold() -> some View {
font(Font.body.bold())
}
}
and apply it to any View (including the TextField
):
TextField("Text", text: $text)
.bold()