I have a SwiftUI expandable FAQ view with Question text that I want bold and Answer text that I want to leave as non-bold text.
I am only able to make either BOTH bold OR both regular weight but want to know if it is possible to selective make the question text bold only??
Below is my code and screen-shot of what I have so far.
FAQ VIEW
struct FAQ: View {
let questionItems : [QuestionAnswer] = [ qa1(), qa2(), qa3() ]
var body: some View {
VStack {
List(questionItems, children: .textTwo) { item in
Text(item.textOne)
//.bold() makes BOTH bold or regular without
.padding([.top, .bottom], 15)
}
.foregroundColor(.black)
.clipped()
}
.navigationBarTitle("Frequently asked questions", displayMode: .inline)
.navigationBarBackButtonHidden(true)
}
}
func qa1() -> QuestionAnswer {
return .init(textOne: "Question one", textTwo: [.init(textOne: "Answer one")])
}
func qa2() -> QuestionAnswer {
return .init(textOne: "Question two", textTwo: [.init(textOne: "Answer two")])
}
func qa3() -> QuestionAnswer {
return .init(textOne: "Question three", textTwo: [.init(textOne: "Answer three")])
}
DATA MODEL
struct QuestionAnswer: Identifiable {
let id = UUID()
let textOne: String
var textTwo: [QuestionAnswer]?
}
4
Answers
You could check if a row has any children to decide whether to bold the text or not.
Or with suggested approach from the comments
You could use this trick from the docs here: https://developer.apple.com/documentation/swiftui/list
Except instead of changing an icon in the description, you would add the ** suggested by @Joakim Danielson
The other approach you can do is below one
you may use attributed string .
Usage:
Use anywhere you need .
for your current problem use attributed string on your list . and you may design as you can .