Created
February 26, 2016 10:46
-
-
Save valeriomazzeo/d26b38ca8a1a06be6280 to your computer and use it in GitHub Desktop.
Updating Height of Self-Sizing Table View Cell With Text View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, [self layoutIfNeeded] in intrinsicContentSize is to force another pass with the correct preferredMaxLayoutWidth, am I right?