需求是這樣的檬寂,文本內(nèi)容高度超過半屏琳钉,那么textview高度就是半屏势木,可以滑動顯示內(nèi)容。內(nèi)容不超過半屏歌懒,就按照內(nèi)容高度設置textview高度啦桌。
開始是使用frame布局的,需要計算每個控件的具體位置及皂,當textview高度變化甫男,textview下面放的button和其他view的frame需要重新賦值,因為它們的y值位置變了验烧。使用這種方法太過麻煩板驳,所以想尋找相對簡單的方法。Masonry約束是大多數(shù)人都在使用的碍拆,約束的方法相當多若治。
但是masonry約束布局有延遲,不是調(diào)用了方法感混,控件的位置就能獲取到的端幼。調(diào)用了masonry去約束控件,再立馬去打印控件的frame浩习,你會發(fā)現(xiàn)静暂,frame還是0济丘。
所以當我設置了以下的約束:
whiteBack = [[UIView alloc] init];
[self.contentView addSubview:whiteBack];
self.resultTextView = [[UITextView alloc] init];
self.resultTextView.font = [UIFont boldSystemFontOfSize:36];
self.resultTextView.textAlignment = NSTextAlignmentCenter;
[whiteBack addSubview:self.resultTextView];
self.voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.voiceButton setImage:kGetImage(@"phrase_detail_voice") forState:UIControlStateNormal];
[self.voiceButton addTarget:self action:@selector(voiceButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[whiteBack addSubview:self.voiceButton];
line = [[UIView alloc] init];
line.backgroundColor = kLineColor;
[whiteBack addSubview:line];
self.originTextView = [[UITextView alloc] init];
self.originTextView.textColor = kHexColor(@"888C98");
self.originTextView.textAlignment = NSTextAlignmentCenter;
self.originTextView.font = [UIFont systemFontOfSize:17];
[whiteBack addSubview:self.originTextView];
[whiteBack mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.equalTo(self.contentView);
}];
[self.resultTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(25);
make.right.offset(-25);
make.top.offset(76);
make.height.mas_equalTo(40);
}];
[self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(whiteBack);
make.top.equalTo(self.resultTextView.mas_bottom).offset(24);
make.size.mas_equalTo(CGSizeMake(60, 30));
}];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(20);
make.right.offset(-20);
make.bottom.equalTo(self.voiceButton.mas_bottom).offset(45);
make.height.mas_equalTo(0.5);
}];
[self.originTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.resultTextView);
make.top.equalTo(line.mas_bottom).offset(50);
make.height.mas_equalTo(40);
}];
// 剛開始沒有添加下面這句
// [whiteBack layoutIfNeeded];
在調(diào)用接口拿到文本內(nèi)容以后谱秽,我去更新textveiw的高度:
- (float)heightForTextView:(UITextView *)textView withFont:(UIFont *)font {
CGSize constraint = CGSizeMake(textView.contentSize.width-textView.textContainer.lineFragmentPadding*2 , CGFLOAT_MAX);
CGRect size = [textView.text boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: font}
context:nil];
float textHeight = ceil(size.size.height) + textView.textContainer.lineFragmentPadding*2;
return textHeight;
}
- (void)resetSubviewsFrame {
CGFloat otherHeight = 245;
CGFloat maxHeight = (whiteBack.height - otherHeight)/2;
CGFloat resultH = [self heightForTextView:self.resultTextView withFont:[UIFont boldSystemFontOfSize:36]];
if (resultH > maxHeight) {
resultH = maxHeight;
}
[self.resultTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(resultH);
}];
CGFloat originH = [self heightForTextView:self.originTextView withFont:[UIFont systemFontOfSize:17]];
if (originH > maxHeight) {
originH = maxHeight;
}
[self.originTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(originH);
}];
[whiteBack layoutIfNeeded];
}
頁面上的兩個textview都沒有顯示出來洽蛀,我來回滑動collectionView幾次,文本才正常顯示出來疟赊。
問題是我計算TextView允許的最大高度時候郊供,使用了它們的父視圖whiteBack的高度,但是使用whiteBack高度時候近哟,它還是0驮审;
解決辦法是在第一次布局時,就使用layoutIfNeeded立即布局吉执,之后再獲取whiteBack的高度就有值了疯淫。