I am working on an application that uses a UITextView.
A UITextView needs to grow or shrink to fit its text, both vertically and horizontally. To do this, I override sizeToFit in the subclass, and I set the bounds as follows:
- (void)sizeToFit { [self setBounds:(CGRect){.size = self.attributedText.size}]; }
The problem is that this size simply does not reflect the correct row size, since the UITextView captures the text. I set cross-inserts to zero, so this should not be a problem?
At this point, I think this is an error using the NSAttributedString size property, but the same thing happens if I use boundingRectWithSize: options: context:
[self setBounds:(CGRect){.size = [self.attributedText boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:0 context:nil].size}];
So itβs possible that no matter what code performs layout calculations for NSAttributedString, it doesnβt play well with UITextView layout calculations.
Here is an example project that demonstrates the problem .
Any ideas are welcome!
EDIT: I should also indicate that the following does not work:
- (void)sizeToFit { [self setBounds:(CGRect){.size = [self sizeThatFits:self.attributedText.size]}]; }
ios objective-c uikit uitextview nsattributedstring
Tom irving
source share