Skip to content

Commit c7f278c

Browse files
authored
Merge pull request #1819 from RomanPodymov/master
Fix #816
2 parents 30c0566 + e107d02 commit c7f278c

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ Version 4.0.0 comes with couple of breaking changes, please refer to [MIGRATION_
335335

336336
## [2.0.0-beta.1](https://github.com/MessageKit/MessageKit/releases/tag/2.0.0-beta.1)
337337

338+
### Fixed
339+
340+
- Fixed `boundingRect(with:options:)` miscalculation of `MessageLabel` by using `NSLayoutManager`, like text `Ꮚ˘̴͈́ꈊ˘̴͈̀Ꮚ⋆✩`、`Tomorrow is the day`.
341+
[#824](https://github.com/MessageKit/MessageKit/pull/824) by [@zhongwuzw](https://github.com/zhongwuzw).
342+
338343
### Changed
339344

340345
- **Breaking Change** Updated codebase to Swift 4.2 [#883](https://github.com/MessageKit/MessageKit/pull/883) by [@nathantannar4](https://github.com/nathantannar4)

Sources/Layout/MessageSizeCalculator.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,33 @@ open class MessageSizeCalculator: CellSizeCalculator {
316316
}
317317

318318
// MARK: Internal
319+
internal lazy var textContainer: NSTextContainer = {
320+
let textContainer = NSTextContainer()
321+
textContainer.maximumNumberOfLines = 0
322+
textContainer.lineFragmentPadding = 0
323+
return textContainer
324+
}()
325+
internal lazy var layoutManager: NSLayoutManager = {
326+
let layoutManager = NSLayoutManager()
327+
layoutManager.addTextContainer(textContainer)
328+
return layoutManager
329+
}()
330+
internal lazy var textStorage: NSTextStorage = {
331+
let textStorage = NSTextStorage()
332+
textStorage.addLayoutManager(layoutManager)
333+
return textStorage
334+
}()
319335

320336
internal func labelSize(for attributedText: NSAttributedString, considering maxWidth: CGFloat) -> CGSize {
321337
let constraintBox = CGSize(width: maxWidth, height: .greatestFiniteMagnitude)
322-
let rect = attributedText.boundingRect(
323-
with: constraintBox,
324-
options: [.usesLineFragmentOrigin, .usesFontLeading],
325-
context: nil).integral
326338

327-
return rect.size
339+
textContainer.size = constraintBox
340+
textStorage.replaceCharacters(in: NSRange(location: 0, length: textStorage.length), with: attributedText)
341+
layoutManager.ensureLayout(for: textContainer)
342+
343+
let size = layoutManager.usedRect(for: textContainer).size
344+
345+
return CGSize(width: size.width.rounded(.up), height: size.height.rounded(.up))
328346
}
329347
}
330348

Sources/Views/MessageLabel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ open class MessageLabel: UILabel {
130130

131131
open override func drawText(in rect: CGRect) {
132132
let insetRect = rect.inset(by: textInsets)
133-
textContainer.size = CGSize(width: insetRect.width, height: rect.height)
133+
textContainer.size = CGSize(width: insetRect.width, height: insetRect.height)
134134

135135
let origin = insetRect.origin
136136
let range = layoutManager.glyphRange(for: textContainer)

0 commit comments

Comments
 (0)