一抹沪、Masonry實現(xiàn)自適應(yīng)高度
/// 關(guān)鍵代碼
CGFloat MIN_HEIGHT = 28.0f;
CGFloat MAX_HEIGHT = 60.0f;
__block UIView *textContainerView = nil;
[self.textView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:NSClassFromString(@"_UITextContainerView")]) {
textContainerView = obj;
*stop = YES;
}
}];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(TOP_HEIGHT+50);
make.left.offset(50);
make.right.offset(-50);
make.height.mas_equalTo(textContainerView).priorityLow();
make.height.mas_greaterThanOrEqualTo(MIN_HEIGHT);
make.height.mas_lessThanOrEqualTo(MAX_HEIGHT);
}];
二另玖、子類化實現(xiàn)自適應(yīng)高度
/// 繼承UITextView后重寫下面方法
/// 關(guān)鍵代碼
- (void)setContentSize:(CGSize)contentSize {
[self invalidateIntrinsicContentSize];
super.contentSize = contentSize;
}
- (CGSize)intrinsicContentSize {
CGFloat MIN_HEIGHT = 28.0f;
CGFloat MAX_HEIGHT = 60.0f;
if (self.contentSize.height <= MIN_HEIGHT) {
return CGSizeMake(self.contentSize.width, MIN_HEIGHT);
}
if (self.contentSize.height >= MAX_HEIGHT) {
return CGSizeMake(self.contentSize.width, MAX_HEIGHT);
}
return self.contentSize;
}
/// 使用如下轿衔,設(shè)置相應(yīng)約束即可
[self.view addSubview:self.textView];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(TOP_HEIGHT+50);
make.left.offset(50);
make.right.offset(-50);
}];
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者