Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let initialText = "<p>FirstParagraph1\nFirstParagraph2</p> Rest"
- print("Taget:\n\(initialText)")
- let attributedString = try! NSMutableAttributedString.init(data: initialText.data(using: .utf8)!,
- options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html],
- documentAttributes: nil)
- print("attributedString:\n\(attributedString)")
- let initialHTMLRenderingData = try! attributedString.data(from: NSRange(location: 0, length: attributedString.length),
- documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html])
- let defaultHTMLString = String(data: initialHTMLRenderingData, encoding: .utf8)
- print("defaultHTMLString:\n\(defaultHTMLString)")
- attributedString.enumerateAttribute(NSAttributedStringKey.paragraphStyle,
- in: NSRange(location: 0, length: attributedString.length),
- options: []) { (paragraph, range, pointee) in
- if (paragraph as? NSParagraphStyle) != nil {
- let occurence = attributedString.attributedSubstring(from: range).string
- let replacement = "<p>" + occurence + "</p>"
- attributedString.replaceCharacters(in: range, with: replacement)
- }
- }
- print("attributedString.string:\n\(attributedString.string)")
- let withBreakLines = attributedString.string.replacingOccurrences(of: "\n", with: "<\\br>")
- print("withBreakLines:\n\(withBreakLines)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement