Home > Software design >  How to get the text from an AttributedString initialized with markdown?
How to get the text from an AttributedString initialized with markdown?

Time:10-05

Using AttributeString that is inited with markdown:

AttributedString(styledMarkdown: "# header")

How can I get just only the text header?

This fails XCTAssertEqual(attributedString, "header") because attributedString is

header {
    NSPresentationIntent = [header 1 (id 1)]
}

CodePudding user response:

NSAttributedString (in UIIKit/TextKit has a string property.

You can go from NSAttributedString to AttributedString to do so:

let nsAttributedString = NSAttributedString(attributedString)
let stringWithoutTags = nsAttributedString.string

You can also use characters of AttributedString:

let stringWithoutTags = String(attributedString.characters)
  • Related