This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from nodes-ios/feature/html-to-nsattributedstring
Added extension for html -> NSAttributedString
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// String+HTML.swift | ||
// Codemine | ||
// | ||
// Created by Chris on 28/06/2017. | ||
// Copyright © 2017 Nodes. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public func stringFromHtml(string: String) -> NSAttributedString? { | ||
do { | ||
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) | ||
if let d = data { | ||
let str = try NSAttributedString(data: d, | ||
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], | ||
documentAttributes: nil) | ||
return str | ||
} | ||
} catch { | ||
print(error) | ||
} | ||
return nil | ||
} | ||
|
||
extension UILabel { | ||
public convenience init(htmlString: String) { | ||
self.init() | ||
numberOfLines = 0 | ||
lineBreakMode = .byWordWrapping | ||
attributedText = htmlString.attributedHTMLString | ||
} | ||
} | ||
|
||
extension String { | ||
public var attributedHTMLString: NSAttributedString? { | ||
return stringFromHtml(string: self) | ||
} | ||
} |