用 boundingRectWithSize 包装 UITextView:
Wrapping UITextView with boundingRectWithSize:
我正在尝试使用以下代码动态更新 UITextView 的高度,但结果始终偏离几个像素,这导致文本视图换行但大小不合适。输入另一个(或两个)字符后,显示会相应更新。
我查看了各种帖子(许多帖子已过时,因为它们引用了已弃用的 sizeThatFits),但我没有看到任何不同之处。那些使用 boundingRectWithSize: 的(NSString 或 NSAttributedString——我都试过了)看起来像下面这样:
CGRect boundingRect = [string boundingRectWithSize: CGSizeMake(CGRectGetWidth(textView.frame), CGFLOAT_MAX)
options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes: @{
NSFontAttributeName : textView.font
}
context: NULL];
float h = ceil(CGRectGetHeight(boundingRect));
textViewHeightLayoutConstraint.constant = h;
我知道您首先要说的是:插图。这是我的设置代码:
textView.textContainerInset = UIEdgeInsetsZero;
textView.contentInset = UIEdgeInsetsZero;
textView.layoutMargins = UIEdgeInsetsZero;
还有insets设置吗?
旁注:我使用 MuseoSans 作为字体。
黑客解决方案
我 "fixed" 通过检查宽度是否在阈值范围内(目前测试结果为 9px)解决了这个问题,如果是,就假装像这样添加了一条额外的线:
float fontHeight = ceil([@"lp" sizeWithAttributes: @{
NSFontAttributeName : textView.font
}].height);
if ((CGRectGetWidth(textView.frame) - ceil(CGRectGetWidth(boundingRect))) <= 9.f)
h += fontHeight;
以上允许我捕获最大可能的高度,然后在我的文本视图上强制它的高度。文本视图确实换行并在下一行显示文本。
想法?
小更新
添加这个没有效果:
NSMutableParagraphStyle *paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
CGRect boundingRect = [string boundingRectWithSize: CGSizeMake(CGRectGetWidth(textView.frame), CGFLOAT_MAX)
options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes: @{
NSFontAttributeName : textView.font,
NSParagraphStyleAttributeName : paragraphStyle
}
context: NULL];
我找到了答案。它位于:
textView.textContainer.lineFragmentPadding
我需要从我的 textView 宽度中减去它。我想除了 3 组插图和边距之外还有另一个填充来源。
我正在尝试使用以下代码动态更新 UITextView 的高度,但结果始终偏离几个像素,这导致文本视图换行但大小不合适。输入另一个(或两个)字符后,显示会相应更新。
我查看了各种帖子(许多帖子已过时,因为它们引用了已弃用的 sizeThatFits),但我没有看到任何不同之处。那些使用 boundingRectWithSize: 的(NSString 或 NSAttributedString——我都试过了)看起来像下面这样:
CGRect boundingRect = [string boundingRectWithSize: CGSizeMake(CGRectGetWidth(textView.frame), CGFLOAT_MAX)
options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes: @{
NSFontAttributeName : textView.font
}
context: NULL];
float h = ceil(CGRectGetHeight(boundingRect));
textViewHeightLayoutConstraint.constant = h;
我知道您首先要说的是:插图。这是我的设置代码:
textView.textContainerInset = UIEdgeInsetsZero;
textView.contentInset = UIEdgeInsetsZero;
textView.layoutMargins = UIEdgeInsetsZero;
还有insets设置吗?
旁注:我使用 MuseoSans 作为字体。
黑客解决方案
我 "fixed" 通过检查宽度是否在阈值范围内(目前测试结果为 9px)解决了这个问题,如果是,就假装像这样添加了一条额外的线:
float fontHeight = ceil([@"lp" sizeWithAttributes: @{
NSFontAttributeName : textView.font
}].height);
if ((CGRectGetWidth(textView.frame) - ceil(CGRectGetWidth(boundingRect))) <= 9.f)
h += fontHeight;
以上允许我捕获最大可能的高度,然后在我的文本视图上强制它的高度。文本视图确实换行并在下一行显示文本。
想法?
小更新
添加这个没有效果:
NSMutableParagraphStyle *paragraphStyle = [NSParagraphStyle.defaultParagraphStyle mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
CGRect boundingRect = [string boundingRectWithSize: CGSizeMake(CGRectGetWidth(textView.frame), CGFLOAT_MAX)
options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes: @{
NSFontAttributeName : textView.font,
NSParagraphStyleAttributeName : paragraphStyle
}
context: NULL];
我找到了答案。它位于:
textView.textContainer.lineFragmentPadding
我需要从我的 textView 宽度中减去它。我想除了 3 组插图和边距之外还有另一个填充来源。