在我的项目中,我必须根据数据动态调整 UItextview 和 UIScrollView 我尝试了很多,但完美的结果还没有到来,请帮助我。
查看我的基于数据增长textview的代码,我不知道如何基于数据增长UIScrollView,请有人帮助我。
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
最佳答案
您可以将textview框架的大小设置为
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame=frame;
或者您可以尝试下面的代码。
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
或者您甚至可以尝试下面的代码。
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
关于ios - 如何根据数据动态调整UITextView和scrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333822/