Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI List header and subheader

Tags:

ios

swift

swiftui

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?

like image 759
Quinn Avatar asked Nov 01 '25 12:11

Quinn


1 Answers

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.

like image 186
Mojtaba Hosseini Avatar answered Nov 03 '25 03:11

Mojtaba Hosseini