48

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Abhishek Biswas
  • 1,125
  • 1
  • 13
  • 19

6 Answers6

92

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
Cary Champlin
  • 1,021
  • 1
  • 7
  • 8
  • Best answer also supporting dynamic type. If possible do not used fixed font sizes. – smat88dd Nov 02 '21 at 02:56
  • 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. – Calin Dec 03 '22 at 01:39
32

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

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
  • 1
    This is unfortunately breaking dynamic type by using a fixed font size, not recommended. – smat88dd Nov 02 '21 at 02:56
  • 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. – Trevor Apr 14 '22 at 17:15
22

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_")
mahan
  • 12,366
  • 5
  • 48
  • 83
21
TextField("Name", text: $name)
    .font(Font.body.bold())
shawnynicole
  • 791
  • 9
  • 7
11

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

.fontWeight(.bold)
A.I.A.
  • 173
  • 2
  • 9
  • 3
    This doesn't work on `TextField`, so it's not really an answer to the question. – pawello2222 Jan 18 '21 at 23:25
  • 2
    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. Feb 23 '21 at 00:04
2

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()
pawello2222
  • 46,897
  • 22
  • 145
  • 209