forked from organicmaps/organicmaps
[iOS] fix html formatting in PP bookmark description
This commit is contained in:
parent
49c2cc0408
commit
2c783fce7f
2 changed files with 40 additions and 19 deletions
|
@ -8,4 +8,30 @@ extension NSAttributedString {
|
|||
text.addAttributes(attributes, range: NSMakeRange(0, text.length))
|
||||
return text
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension NSMutableAttributedString {
|
||||
@objc convenience init?(htmlString: String, baseFont: UIFont) {
|
||||
guard let data = htmlString.data(using: .utf8) else { return nil }
|
||||
|
||||
do {
|
||||
try self.init(data: data,
|
||||
options: [.documentType : NSAttributedString.DocumentType.html,
|
||||
.characterEncoding: String.Encoding.utf8.rawValue],
|
||||
documentAttributes: nil)
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
|
||||
enumerateAttribute(.font, in: NSMakeRange(0, length), options: []) { (value, range, _) in
|
||||
if let font = value as? UIFont,
|
||||
let descriptor = baseFont.fontDescriptor.withSymbolicTraits(font.fontDescriptor.symbolicTraits) {
|
||||
let newFont = UIFont(descriptor: descriptor, size: baseFont.pointSize)
|
||||
addAttribute(.font, value: newFont, range: range)
|
||||
} else {
|
||||
addAttribute(.font, value: baseFont, range: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#import "MWMBookmarkCell.h"
|
||||
#import "MWMPlacePageCellUpdateProtocol.h"
|
||||
#import "MWMPlacePageProtocol.h"
|
||||
#import "SwiftBridge.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -36,6 +37,7 @@ NSString * const kTextViewContentSizeKeyPath = @"contentSize";
|
|||
{
|
||||
[super awakeFromNib];
|
||||
[self registerObserver];
|
||||
self.textView.textContainer.lineFragmentPadding = 0;
|
||||
}
|
||||
|
||||
- (void)dealloc { [self unregisterObserver]; }
|
||||
|
@ -126,29 +128,22 @@ NSString * const kTextViewContentSizeKeyPath = @"contentSize";
|
|||
[self stateOpen:YES];
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
NSDictionary<NSString *, id> * attr = @{
|
||||
NSForegroundColorAttributeName : [UIColor blackPrimaryText],
|
||||
NSFontAttributeName : [UIFont regular16]
|
||||
};
|
||||
NSError * error = nil;
|
||||
NSData * data = [text dataUsingEncoding:NSUnicodeStringEncoding];
|
||||
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]
|
||||
initWithData:data
|
||||
options:@{
|
||||
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType
|
||||
}
|
||||
documentAttributes:nil
|
||||
error:&error];
|
||||
if (error)
|
||||
auto font = [UIFont regular16];
|
||||
auto color = [UIColor blackPrimaryText];
|
||||
auto str = [[NSMutableAttributedString alloc] initWithHtmlString:text baseFont:font];
|
||||
if (str)
|
||||
{
|
||||
// If we failed while attempting to render html than just show plain text in bookmark.
|
||||
// Usually there is a problem only in iOS7.
|
||||
self.attributedHTML = [[NSAttributedString alloc] initWithString:text attributes:attr];
|
||||
[str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)];
|
||||
self.attributedHTML = str;
|
||||
}
|
||||
else
|
||||
{
|
||||
[str addAttributes:attr range:{0, str.length}];
|
||||
self.attributedHTML = str;
|
||||
self.attributedHTML =
|
||||
[[NSAttributedString alloc] initWithString:text
|
||||
attributes:@{
|
||||
NSFontAttributeName : font,
|
||||
NSForegroundColorAttributeName : color
|
||||
}];
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
|
|
Loading…
Add table
Reference in a new issue