accessibilityTextHeadingLevel not working in NSAttributedString

Hey guys,

I have an NSAttributedString within my app (created from HTML). I assign this string to a UITextView. I would like certain parts of that text to be marked with an 'header' accessibility trait (all the headlines in that text) so that voice over can identify them properly.

I was under the impression that I can just use accessibilityTextHeadingLevel to do so, but the text in that given range is still setup with the 'text' accessibility trait:

var myString = NSMutableAttributedString(...) 	
let range = NSRange(location: 0, length: 44)				myString.addAttribute(NSAttributedString.Key.accessibilityTextHeadingLevel, value: 1, range: range)

How is accessibilityTextHeadingLevel supposed to work?

  • I have a similar question. I have UILabels which I use as headings (setting their accessibilityTraits to .header). I thought that I could establish some sort of header hierarchy by setting each one's attributedText to an attributed string which had .accessibilityTextHeadingLevel applied to its entire range ... but that doesn't appear to result in any difference in the accessibility experience.

    What effect is .accessibilityTextHeadingLevel supposed to have? I see documentation on how to set it, but nothing saying what it actually does.

  • In swiftUI we can achieve solution easily using AccessibilityHeadingLevel.h1, it would be helpful if some helps here to achieve the same using swift.

Add a Comment

Replies

Hi Team, We are also facing the same issue, accessibilityTextHeadingLevel is not working,

     let attrHeaderText1 = NSMutableAttributedString(string: "Page Title")
     attrHeaderText1.addAttribute(NSAttributedString.Key.accessibilityTextHeadingLevel, 
                                     value: 3,
                                     range: NSRange(location: 0, length: 4))
     lblofH1.attributedText = attrHeaderText1
     lblofH1.accessibilityTraits = UIAccessibilityTraits.header

Any other ways to apply HeadingLevel?

Thank you InAdvance.