Skip to content

Commit

Permalink
Replace attributed string height calculation with a more reliable imp…
Browse files Browse the repository at this point in the history
…lementation
  • Loading branch information
stefanceriu committed Sep 12, 2022
1 parent bcb73e9 commit 81773cd
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Riot/Modules/MatrixKit/Models/Room/MXKRoomBubbleCellData.m
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,7 @@ - (CGSize)textContentSize:(NSAttributedString*)attributedText removeVerticalInse
CGFloat verticalInset = measurementTextView.textContainerInset.top + measurementTextView.textContainerInset.bottom;
CGFloat horizontalInset = measurementTextView.textContainer.lineFragmentPadding * 2;

CGSize size = [attributedText boundingRectWithSize:CGSizeMake(_maxTextViewWidth - horizontalInset, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingUsesDeviceMetrics
context:nil].size;

//In iOS 7 and later, this method returns fractional sizes (in the size component of the returned rectangle);
// to use a returned size to size views, you must use raise its value to the nearest higher integer using the
// [ceil](https://developer.apple.com/documentation/kernel/1557272-ceil?changes=latest_major) function.
size.width = ceil(size.width);
size.height = ceil(size.height);
CGSize size = [self sizeForAttributedString:attributedText fittingWidth:_maxTextViewWidth - horizontalInset];

// The result is expected to contain the textView textContainer's paddings. Add them back if necessary
if (removeVerticalInset == NO) {
Expand All @@ -553,6 +545,27 @@ - (CGSize)textContentSize:(NSAttributedString*)attributedText removeVerticalInse
return size;
}

// https://stackoverflow.com/questions/54497598/nsattributedstring-boundingrect-returns-wrong-height
- (CGSize)sizeForAttributedString:(NSAttributedString *)attributedString fittingWidth:(CGFloat)width
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

CGRect boundingRect = CGRectMake(0.0, 0.0, width, CGFLOAT_MAX);

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:boundingRect.size];
textContainer.lineFragmentPadding = 0;

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer: textContainer];

[textStorage addLayoutManager:layoutManager];
[layoutManager glyphRangeForBoundingRect:boundingRect inTextContainer:textContainer];

CGRect rect = [layoutManager usedRectForTextContainer:textContainer];

return CGRectIntegral(rect).size;
}

#pragma mark - Properties

- (MXSession*)mxSession
Expand Down

0 comments on commit 81773cd

Please sign in to comment.