From 72eae517c4d84cfc88a4e1d8cf8ad548be508235 Mon Sep 17 00:00:00 2001 From: Aleksey Belousov Date: Mon, 8 Jun 2020 14:54:53 +0300 Subject: [PATCH] [iOS] resize images in html description of object to fit PP width https://jira.mail.ru/browse/MAPSME-13740 --- .../Maps/Categories/NSAttributedString+HTML.swift | 5 ++++- .../PlacePageBookmarkViewController.swift | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/iphone/Maps/Categories/NSAttributedString+HTML.swift b/iphone/Maps/Categories/NSAttributedString+HTML.swift index aa9a26a24e..55ec9d1d67 100644 --- a/iphone/Maps/Categories/NSAttributedString+HTML.swift +++ b/iphone/Maps/Categories/NSAttributedString+HTML.swift @@ -13,11 +13,14 @@ extension NSAttributedString { } extension NSMutableAttributedString { - @objc convenience init?(htmlString: String, baseFont: UIFont, paragraphStyle: NSParagraphStyle?) { + @objc convenience init?(htmlString: String, baseFont: UIFont, paragraphStyle: NSParagraphStyle?, estimatedWidth: CGFloat = 0) { self.init(htmlString: htmlString, baseFont: baseFont) if let paragraphStyle = paragraphStyle { addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, length)) } + + guard estimatedWidth > 0 else { return } + enumerateAttachments(estimatedWidth: estimatedWidth) } @objc convenience init?(htmlString: String, baseFont: UIFont) { diff --git a/iphone/Maps/UI/PlacePage/Components/PlacePageBookmarkViewController.swift b/iphone/Maps/UI/PlacePage/Components/PlacePageBookmarkViewController.swift index 84b91114bf..6bcf068343 100644 --- a/iphone/Maps/UI/PlacePage/Components/PlacePageBookmarkViewController.swift +++ b/iphone/Maps/UI/PlacePage/Components/PlacePageBookmarkViewController.swift @@ -29,12 +29,20 @@ class PlacePageBookmarkViewController: UIViewController { updateViews() } + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + guard let bookmarkData = bookmarkData, + bookmarkData.isHtmlDescription, + expandableLabel.width > 0, + let description = bookmarkData.bookmarkDescription else { return } + setHtmlDescription(description, maxWidth: expandableLabel.width) + } + func updateViews() { guard let bookmarkData = bookmarkData else { return } editButton.isEnabled = bookmarkData.isEditable if let description = bookmarkData.bookmarkDescription { if bookmarkData.isHtmlDescription { - setHtmlDescription(description) topConstraint.constant = 16 } else { expandableLabel.text = description @@ -45,7 +53,7 @@ class PlacePageBookmarkViewController: UIViewController { } } - private func setHtmlDescription(_ htmlDescription: String) { + private func setHtmlDescription(_ htmlDescription: String, maxWidth: CGFloat) { DispatchQueue.global().async { let font = UIFont.regular14() let color = UIColor.blackPrimaryText() @@ -53,7 +61,7 @@ class PlacePageBookmarkViewController: UIViewController { paragraphStyle.lineSpacing = 4 let attributedString: NSAttributedString - if let str = NSMutableAttributedString(htmlString: htmlDescription, baseFont: font, paragraphStyle: paragraphStyle) { + if let str = NSMutableAttributedString(htmlString: htmlDescription, baseFont: font, paragraphStyle: paragraphStyle, estimatedWidth: maxWidth) { str.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: NSRange(location: 0, length: str.length))