PSB-196 fix message bubbles by removing timestamps

This commit is contained in:
Frank Rotermund
2022-11-15 15:26:53 +01:00
parent 3a6c5d5b9f
commit ab8c98183d
3 changed files with 43 additions and 57 deletions
@@ -518,52 +518,48 @@
- (CGSize)textContentSize:(NSAttributedString*)attributedText removeVerticalInset:(BOOL)removeVerticalInset
{
if (attributedText.length == 0) {
return CGSizeZero;
}
// Grab the default textContainer insets and lineFragmentPadding from a dummy text view.
// This has no business being here but the refactoring effort would be too great (sceriu 05.09.2022)
static UITextView* measurementTextView = nil;
if (!measurementTextView)
static UITextView* measurementTextViewWithoutInset = nil;
if (attributedText.length)
{
measurementTextView = [[UITextView alloc] init];
if (!measurementTextView)
{
measurementTextView = [[UITextView alloc] init];
measurementTextViewWithoutInset = [[UITextView alloc] init];
// Remove the container inset: this operation impacts only the vertical margin.
// Note: consider textContainer.lineFragmentPadding to remove horizontal margin
measurementTextViewWithoutInset.textContainerInset = UIEdgeInsetsZero;
}
// Select the right text view for measurement
UITextView *selectedTextView = (removeVerticalInset ? measurementTextViewWithoutInset : measurementTextView);
selectedTextView.frame = CGRectMake(0, 0, _maxTextViewWidth, 0);
selectedTextView.attributedText = attributedText;
// Force the layout manager to layout the text, fixes problems starting iOS 16
[selectedTextView.layoutManager ensureLayoutForTextContainer:selectedTextView.textContainer];
CGSize size = [selectedTextView sizeThatFits:selectedTextView.frame.size];
// Manage the case where a string attribute has a single paragraph with a left indent
// In this case, [UITextView sizeThatFits] ignores the indent and return the width
// of the text only.
// So, add this indent afterwards
NSRange textRange = NSMakeRange(0, attributedText.length);
NSRange longestEffectiveRange;
NSParagraphStyle *paragraphStyle = [attributedText attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:&longestEffectiveRange inRange:textRange];
if (NSEqualRanges(textRange, longestEffectiveRange))
{
size.width = size.width + paragraphStyle.headIndent;
}
return size;
}
CGFloat verticalInset = measurementTextView.textContainerInset.top + measurementTextView.textContainerInset.bottom;
CGFloat horizontalInset = measurementTextView.textContainer.lineFragmentPadding * 2;
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) {
size.height += verticalInset;
}
size.width += horizontalInset;
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;
return CGSizeZero;
}
#pragma mark - Properties