Skip to content

Instantly share code, notes, and snippets.

@valeriomazzeo
Created February 26, 2016 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valeriomazzeo/d26b38ca8a1a06be6280 to your computer and use it in GitHub Desktop.
Save valeriomazzeo/d26b38ca8a1a06be6280 to your computer and use it in GitHub Desktop.
Updating Height of Self-Sizing Table View Cell With Text View
@interface TView : UITextView
@property (nonatomic) CGFloat preferredMaxLayoutWidth;
@end
@implementation TView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.scrollEnabled = NO;
self.editable = NO;
self.textContainer.lineFragmentPadding = 0.0f;
self.textContainerInset = UIEdgeInsetsZero;
}
return self;
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self invalidateIntrinsicContentSize];
}
- (void)setPreferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth
{
_preferredMaxLayoutWidth = preferredMaxLayoutWidth;
[self invalidateIntrinsicContentSize];
}
- (void)layoutSubviews
{
_preferredMaxLayoutWidth = self.frame.size.width;
[super layoutSubviews];
}
- (CGSize)intrinsicContentSize
{
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, [self sizeThatFits:CGSizeMake(self.preferredMaxLayoutWidth, 0)].height);
}
@end
@Ricardo1980
Copy link

So, [self layoutIfNeeded] in intrinsicContentSize is to force another pass with the correct preferredMaxLayoutWidth, am I right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment