61

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?

6 Answers 6

103

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Best answer also supporting dynamic type. If possible do not used fixed font sizes.
The only problem with this approach is it hardcodes Font.headline, overriding the default. There is .fontWeight() on iOS 16, but for iOS 15 this is probably a reasonable solution.
43

Updated Answer:

 Text("Bold Text").bold() Text("**Bold Text**") Text("*Italic Text*") Text("***Bold Italic Text***") 

enter image description here



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() } } 

enter image description here

2 Comments

This is unfortunately breaking dynamic type by using a fixed font size, not recommended.
What I don't like in this example .font(.system(size: 30, weight: .heavy, design: .default)) is that we MUST define the font size. If we just want to stick guidelines with predefined .title .body kind font sizes this method is not ideal.
25

iOS 15+

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_") 

5 Comments

Important: That's not a Swift feature, that's an iOS feature! It will only work on iOS 15! Users on older versions will see raw Markdown.
Is there a way to weight of the text within these asterisks to something heavier than bold?
@Joseph. No, as of today it is not possible. Use .fontWeight(.black) instead
Works with string literal. How to make it work with a string variable?
21
TextField("Name", text: $name) .font(Font.body.bold()) 

Comments

12

The updated approach for iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+ is:

.fontWeight(.bold) 

6 Comments

This doesn't work on TextField, so it's not really an answer to the question.
My bad. fontWeight is suitable for Text views but not for TextFields since they don't expose their label views. Thus .font(Font.body.bold()) is still the simplest solution :(
@A.I.A. fontWeight is iOS 16+.
@Freeubi fontWeight is for iOS 13+. iOS 16+ is the overriden one that returns Text.
yes. Which is needed in this case.
@Freeubi Sorry, I was wrong
3

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() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.