4

The goal is for me to have a List section with a bold header and non-bold sub-header, which is something I couldn't figure out how to do. So I tried making the header with a Text view that contains text that is partially bold, and partially normal non-bold text.

I tried doing this with an NSAttributedString which works with a UILabel but it doesn't appear to work with swiftUI's Text object.

Im making the header like so:

Section(header: Text(docSection.formattedHeader)) {
    ...

where docSection.formattedHeader is an NSAttributedString that is half bold, half non-bold separated with a \n

however then I get the following error:

Initializer 'init(_:)' requires that 'NSAttributedString' conform to 'StringProtocol'

Is there anyway to achieve this?

1 Answer 1

7

Since NSAttributedString is incompatible with SwiftUI (yet), you should use Text instead. But for Section, You can use any View in. So why don't you use a stackView like this:

Section(header:
    VStack(alignment: .leading) {
        Text("Header").fontWeight(.bold)
        Text("Subheader").fontWeight(.regular)
    }
) {
    Text("Content")
}

Also you can use HStack or any other combined views.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This does the trick, don't know why I thought I could only use text as the header
Maybe because of the old UITableView method: titleForHeaderInSection....
@Quinn do you know how to display header and subheader inside List item? Trying List{ ForEach{ VStack{ Text Text }}} doesn't compile..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.