Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making parts of text bold in SwiftUI

I was wondering how I would make only sections of a text bold while keep the rest 'regular' in SwiftUI.

I currently have:

Text("Coronavirus Disease of 2019")

and I want it to print out COronaVirus Disease of 2019 and haven't been able to get only some parts bold.

like image 467
Thomas Braun Avatar asked May 08 '20 03:05

Thomas Braun


People also ask

What property makes bold text?

The font-weight CSS property sets the weight (or boldness) of the font.


Video Answer


4 Answers

iOS 15+ (Swift 5.5 +)

SwiftUI has built-in support for rendering Markdown.

It is GitHub flavored markdown. AttributedString converts both inline and block styles. SwiftUI renders inline styles (but not images at this time). We use the fantastic cmark-gfm library to parse the markdown string. - SwiftUI Frameworks Engineer - developer.apple.com

See more:

What is Markdown?


Use double asterisks (**) arroud the characters that you want to make bold.

Text("**CO**rona**V**irus **D**isease of 20**19**")

Use underscore (_) arround the charachters you want to make italic.

Text("Is this text _emphasized_?")

String variable

Use init(_ value: String)

Creates a localized string key from the given string value.

let bold = "This text is **bold**"
Text(.init(bold))

String interpolation

Use init(_ value: String)

Creates a localized string key from the given string value.

let bold = "Bold"
Text(.init("This text is **\(bold)**"))

Attributed text

Use init(_ attributedContent: AttributedString)

Creates a text view that displays styled attributed content.

let markdownText = try! AttributedString(markdown: "This text is **bold**")
Text(markdownText)

See also:

init(_ attributedContent: AttributedString) - https://developer.apple.com

like image 70
mahan Avatar answered Oct 19 '22 05:10

mahan


If you don't need to translate it here is possible fast variant

demo

Text("CO").bold() + Text("rona") + Text("VI").bold() + 
    Text("rus Disease of 20") + Text("19").bold()

alternate is to use NSAttributedString with UIViewRepresentable of UILabel.

like image 26
Asperi Avatar answered Oct 19 '22 04:10

Asperi


A quick note just to add onto Asperi's great answer, if you need to apply frame or padding modifiers to your text you'll need to group the text first and then add your modifiers to the group.

Group { Text("CO").bold() + Text("rona") + Text("VI").bold() + Text("rus Disease of 20") + Text("19").bold() }.frame(width: 100, height: 100).padding(.horizontal)
like image 13
LJ White Avatar answered Oct 19 '22 04:10

LJ White


Flavours of this question crop up a lot and for a newcomer to Swift without a background in Objective-C, the solutions emerge grudgingly. Several of the above answers are excellent, but to summarize perhaps the optimal solution to the question as asked,

Group {
    Text("CO").bold() +
    Text("rona") +
    Text("V").bold() +
    Text("irus ") +
    Text("D").bold() +
    Text("isease of 20") +
    Text("19").bold()
}
.font(.caption)
.frame(width: 300)

(Group{} was the secret sauce for me)

like image 4
jws Avatar answered Oct 19 '22 04:10

jws