[iOS] Added image resize for bookmarks and guide descriptions

This commit is contained in:
Zoia Pribytkova 2019-07-25 17:37:10 +03:00 committed by Aleksey Belousov
parent 5647cf74f1
commit 39da2707a6
3 changed files with 60 additions and 7 deletions

View file

@ -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)
{

View file

@ -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
}
}

View file

@ -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)];