From 5b41fbc9bc66b38274b8d3e85d4e2aa6f771bcd3 Mon Sep 17 00:00:00 2001 From: Zoia Pribytkova Date: Tue, 23 Apr 2019 18:30:31 +0300 Subject: [PATCH] [iOS] attributes fixes for the place description --- .../Categories/NSAttributedString+HTML.swift | 7 ++++ .../PlacePageDescriptionViewController.swift | 13 +++++-- .../MWMPlaceDescriptionCell.mm | 34 ++++++++----------- .../MWMPlaceDescriptionCell.xib | 19 ++++++----- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/iphone/Maps/Categories/NSAttributedString+HTML.swift b/iphone/Maps/Categories/NSAttributedString+HTML.swift index 211c4c5055..49ca56c283 100644 --- a/iphone/Maps/Categories/NSAttributedString+HTML.swift +++ b/iphone/Maps/Categories/NSAttributedString+HTML.swift @@ -13,6 +13,13 @@ extension NSAttributedString { } extension NSMutableAttributedString { + @objc convenience init?(htmlString: String, baseFont: UIFont, paragraphStyle: NSParagraphStyle?) { + self.init(htmlString: htmlString, baseFont: baseFont) + if let paragraphStyle = paragraphStyle { + addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, length)) + } + } + @objc convenience init?(htmlString: String, baseFont: UIFont) { guard let data = htmlString.data(using: .utf8) else { return nil } diff --git a/iphone/Maps/UI/PlacePage/PlacePageDescriptionViewController.swift b/iphone/Maps/UI/PlacePage/PlacePageDescriptionViewController.swift index 4e595612ef..9cb32e2716 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageDescriptionViewController.swift +++ b/iphone/Maps/UI/PlacePage/PlacePageDescriptionViewController.swift @@ -12,8 +12,17 @@ final class PlacePageDescriptionViewController: WebViewController { } override func configuredHtml(withText htmlText: String) -> String { - var html = htmlText.replacingOccurrences(of: "", with: "") - html = html.replacingOccurrences(of: "", with: "

wikipedia.org

") + let scale = UIScreen.main.scale + let styleTags = """ + + + + + """ + var html = htmlText.replacingOccurrences(of: "", with: styleTags) + html = html.replacingOccurrences(of: "", with: "

wikipedia.org

") return html } diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.mm b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.mm index cd09a951b4..7fb35c01c4 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.mm +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.mm @@ -12,8 +12,7 @@ @implementation MWMPlaceDescriptionCell -- (void)configureWithDescription:(NSString *)text delegate:(id)delegate -{ +- (void)configureWithDescription:(NSString *)text delegate:(id)delegate { self.delegate = delegate; self.attributedHTML = nil; self.originalText = text; @@ -22,37 +21,34 @@ [self configHTML:text]; } -- (void)configHTML:(NSString *)text -{ - if (self.attributedHTML) - { +- (void)configHTML:(NSString *)text { + if (self.attributedHTML) { if (self.attributedHTML.length <= 500) { self.textView.attributedText = self.attributedHTML; } else { self.textView.attributedText = [self.attributedHTML attributedSubstringFromRange:NSMakeRange(0, 500)]; } - } - else - { + } else { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - auto font = [UIFont regular16]; - auto color = [UIColor blackPrimaryText]; - auto str = [[NSMutableAttributedString alloc] initWithHtmlString:text baseFont:font]; - if (str) - { + UIFont *font = [UIFont regular14]; + UIColor *color = [UIColor blackPrimaryText]; + NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; + style.lineSpacing = 4.0; + NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithHtmlString:text + baseFont:font + paragraphStyle:style]; + if (str) { [str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)]; self.attributedHTML = str; - } - else - { + } else { self.attributedHTML = [[NSAttributedString alloc] initWithString:text attributes:@{ NSFontAttributeName : font, - NSForegroundColorAttributeName : color + NSForegroundColorAttributeName : color, + NSParagraphStyleAttributeName : style }]; } - dispatch_async(dispatch_get_main_queue(), ^{ [self configHTML:nil]; }); diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.xib b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.xib index 8367fd46b1..b3fa6a575d 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.xib +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/DescriptionCell/MWMPlaceDescriptionCell.xib @@ -1,24 +1,24 @@ - + - + - - + + - + - + @@ -37,11 +37,12 @@ -