Very primitive HTML parser oh yeah, one more!
I want to parse a lot of small html-like chunks to a NSAttributedString
in background. So, I can do something like Android guys doing with android.text.Html and then we can do primitive formatted text on the backend...
But I hate to do it with build-in main-thread-only parser from UIKit:
[[NSAttributedString alloc] initWithData:[html dataUsingEncoding: NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding),
NSDefaultAttributesDocumentAttribute:@{
NSFontAttributeName : [UIFont systemFontOfSize:SystemFontSize]
}}
documentAttributes: nil
error: &error];
When I need only that:
<b>Bold:</b> important <i>italic</i> message
Bold: important italic message
Well, not much, as it is designed to be small, fast, one thing
<b>
<i>
<a>
<br>
- very nasty way, though
&
- makes &
- replaced by simple space
OSSimpleHTML *htmlParser = [[OSSimpleHTML alloc] initWithBasicTextAttributes:@{}];
NSAttributedString *labelText = [htmlParser attributedStringFromHTML:@"<b>Bold:</b> important <i>italic</i> message"];
UIFontDescriptor *descriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleHeadline];
UIFont *font = [UIFont fontWithDescriptor:descriptor size:descriptor.pointSize];
OSSimpleHTML *htmlParser = [[OSSimpleHTML alloc] initWithBasicTextAttributes:@{
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : font
}];
- Not yet. But you can send a MR =^^=