From 39da2707a6a6b76151d0dfd4bee04c757ca79f44 Mon Sep 17 00:00:00 2001 From: Zoia Pribytkova Date: Thu, 25 Jul 2019 17:37:10 +0300 Subject: [PATCH] [iOS] Added image resize for bookmarks and guide descriptions --- iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm | 9 ++-- .../Categories/NSAttributedString+HTML.swift | 54 +++++++++++++++++-- .../Content/BookmarkCell/MWMBookmarkCell.mm | 4 +- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm index 5486c73b44..838ee9df0a 100644 --- a/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm +++ b/iphone/Maps/Bookmarks/MWMCategoryInfoCell.mm @@ -55,9 +55,12 @@ self.authorLabel.text = [NSString stringWithCoreFormat:L(@"author_name_by_prefix") arguments:@[@(data.m_authorName.c_str())]]; auto infoHtml = @(GetPreferredBookmarkStr(data.m_description).c_str()); - auto info = [NSAttributedString stringWithHtml:infoHtml - defaultAttributes: @{NSFontAttributeName : [UIFont regular14], - NSForegroundColorAttributeName: [UIColor blackPrimaryText]}]; + auto info = [[NSMutableAttributedString alloc] initWithHtmlString:infoHtml + baseFont:[UIFont regular14] + estimatedWidth:[UIScreen mainScreen].bounds.size.width - 32.0f]; + [info addAttribute: NSForegroundColorAttributeName + value:[UIColor blackPrimaryText] + range:NSMakeRange(0, [info length])]; auto shortInfo = @(GetPreferredBookmarkStr(data.m_annotation).c_str()); if (info.length > 0 && shortInfo.length > 0) { diff --git a/iphone/Maps/Categories/NSAttributedString+HTML.swift b/iphone/Maps/Categories/NSAttributedString+HTML.swift index 49ca56c283..a629f7a3d5 100644 --- a/iphone/Maps/Categories/NSAttributedString+HTML.swift +++ b/iphone/Maps/Categories/NSAttributedString+HTML.swift @@ -32,14 +32,62 @@ extension NSMutableAttributedString { return nil } + enumerateFont(baseFont) + } + + @objc convenience init?(htmlString: String, baseFont: UIFont, estimatedWidth: CGFloat) { + 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 + } + + enumerateFont(baseFont) + + guard estimatedWidth > 0 else { return } + + enumerateAttachments(estimatedWidth: estimatedWidth) + } + + func enumerateFont(_ font: UIFont) { 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) + let descriptor = font.fontDescriptor.withSymbolicTraits(font.fontDescriptor.symbolicTraits) { + let newFont = UIFont(descriptor: descriptor, size: font.pointSize) addAttribute(.font, value: newFont, range: range) } else { - addAttribute(.font, value: baseFont, range: range) + addAttribute(.font, value: font, range: range) } } } + + func enumerateAttachments(estimatedWidth: CGFloat) { + enumerateAttribute(.attachment, in: NSMakeRange(0, length), options: []) { (value, range, _) in + if let attachement = value as? NSTextAttachment { + let image = attachement.image(forBounds: attachement.bounds, textContainer: NSTextContainer(), characterIndex: range.location)! + if image.size.width > estimatedWidth { + let newImage = resizeImage(image: image, scale: estimatedWidth/image.size.width) ?? image + let resizedAttachment = NSTextAttachment() + resizedAttachment.image = newImage + addAttribute(.attachment, value: resizedAttachment, range: range) + } + } + } + } + + func resizeImage(image: UIImage, scale: CGFloat) -> UIImage? { + let newSize = CGSize(width: image.size.width*scale, height: image.size.height*scale) + let rect = CGRect(origin: CGPoint.zero, size: newSize) + + let renderer = UIGraphicsImageRenderer(size: newSize) + let newImage = renderer.image { context in + image.draw(in: rect) + } + return newImage + } } diff --git a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/BookmarkCell/MWMBookmarkCell.mm b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/BookmarkCell/MWMBookmarkCell.mm index d12bfa2069..c0f77a53e2 100644 --- a/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/BookmarkCell/MWMBookmarkCell.mm +++ b/iphone/Maps/UI/PlacePage/PlacePageLayout/Content/BookmarkCell/MWMBookmarkCell.mm @@ -130,7 +130,9 @@ NSString * const kTextViewContentSizeKeyPath = @"contentSize"; 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]; + auto str = [[NSMutableAttributedString alloc] initWithHtmlString:text + baseFont:font + estimatedWidth:[UIScreen mainScreen].bounds.size.width - 32.0f]; if (str) { [str addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)];