From e63091c18cd58b837f3dc2bae80f88e882e23a10 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Wed, 22 Aug 2018 17:54:48 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20boundingRect(with:options:)=20miscalcul?= =?UTF-8?q?ation=20of=20MessageLabel=20like=20text=20=E1=8F=8A=CB=98=CC=B4?= =?UTF-8?q?=CD=88=CC=81=EA=88=8A=CB=98=CC=B4=CD=88=CC=80=E1=8F=8A=E2=8B=86?= =?UTF-8?q?=E2=9C=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++++ Sources/Layout/MessageSizeCalculator.swift | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cd7961a..1195ab39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ## Upcoming release +### Fixed + +- Fixed `boundingRect(with:options:)` miscalculation of `MessageLabel` , like text `Ꮚ˘̴͈́ꈊ˘̴͈̀Ꮚ⋆✩`、`Tomorrow is the day`. +[#824](https://github.com/MessageKit/MessageKit/pull/824) by [@zhongwuzw](https://github.com/zhongwuzw). + ### Changed - The `MessageData.emoji` case once again uses a default font of 2x the `messageLabelFont` size. diff --git a/Sources/Layout/MessageSizeCalculator.swift b/Sources/Layout/MessageSizeCalculator.swift index 7e563629..ef7a5ae3 100644 --- a/Sources/Layout/MessageSizeCalculator.swift +++ b/Sources/Layout/MessageSizeCalculator.swift @@ -24,6 +24,8 @@ import Foundation +private let additionalWidthForBoundingRectCalculation: CGFloat = 5.0 + open class MessageSizeCalculator: CellSizeCalculator { public init(layout: MessagesCollectionViewFlowLayout? = nil) { @@ -216,8 +218,9 @@ open class MessageSizeCalculator: CellSizeCalculator { internal func labelSize(for attributedText: NSAttributedString, considering maxWidth: CGFloat) -> CGSize { let constraintBox = CGSize(width: maxWidth, height: .greatestFiniteMagnitude) let rect = attributedText.boundingRect(with: constraintBox, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).integral - - return rect.size + + // `boundingRect` method maybe not calculate correctly, like `Ꮚ˘̴͈́ꈊ˘̴͈̀Ꮚ⋆✩`, so we add 5 points to fix them temporary. + return CGSize(width: rect.width + additionalWidthForBoundingRectCalculation, height: rect.height) } }