I am building a RSS app, and am working on displaying all of the content inside the app, not in a WebView
. I am trying to figure out how to create custom styles for each HTML element in a UILabel
.
HTML text in UILabel with Attributed String:
I know that this can be done with a NSAttributedString
. I've been able to display the HTML text in the UILabel
(see screenshot) through an attributed string:
do {
let attrStr = try NSAttributedString(
data: (content.data(using: String.Encoding.unicode, allowLossyConversion: true)!),
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
bodyLabel.attributedText = attrStr
} catch let error {
print(error)
}
However I can not figure out how to set the text in between each tag to have the specific style associated with that tag. Here is an example of the HTML code I am trying to style:
<p>Back in 2006, when the iPhone was a mere rumor, Palm CEO Ed Colligan was asked if he
was worried:</p>
<blockquote>
<p>“We’ve learned and struggled for a few years here figuring out how to
make a decent phone,” he said. “PC guys are not going to just figure this
out. They’re not going to just walk in.” What if Steve Jobs’
company did bring an iPod phone to market? Well, it would probably use WiFi
technology and could be distributed through the Apple stores and not the carriers
like Verizon or Cingular, Colligan theorized.</p>
</blockquote>
<p>I was reminded of this quote after Amazon <a href=
"http://phx.corporate-ir.net/phoenix.zhtml?c=176060&p=irol-newsArticle&ID=2281414">
announced an agreement to buy Whole Foods</a> for $13.7 billion; after all, it was only
<a href=
"https://www.bloomberg.com/news/articles/2015-01-29/in-shift-whole-foods-to-compete-with-price-cuts-loyalty-app">
two years ago</a> that Whole Foods founder and CEO John Mackey predicted that groceries
would be Amazon’s Waterloo. And while Colligan’s prediction was far worse
— Apple simply left Palm in the dust, unable to compete — it is Mackey who
has to call Amazon founder and CEO Jeff Bezos, the Napoleon of this little morality
play, boss.</p>
EDIT: So I've made some progress. The original problem of isolating the inside of specific HTML elements for styling still persists, but I have gotten to the point where this is the only problem.
if let htmlText = content.htmlAttributedString {
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 19.0)]
let attributedText = NSMutableAttributedString(string: htmlText.string, attributes: attributes)
let htmlString = htmlText.string as NSString
let range = htmlString.range(of: title)
attributedText.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFont(ofSize: 24.0), range: range)
bodyLabel.attributedText = attributedText
}
var htmlAttributedString: NSAttributedString? {
do {
let attrStr = try NSAttributedString(
data: (self.data(using: String.Encoding.unicode, allowLossyConversion: true)!),
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attrStr
} catch let error {
print(error)
}
return nil
}