如何基于文本计算TextView高度 [英] How to calculate TextView height base on text

查看:53
本文介绍了如何基于文本计算TextView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码计算文本的高度,然后为 UILabel UITextView

I am using the code below for calculate the height of text, then set this height for UILabel and UITextView

CGSize targetSize = CGSizeMake(300, CGFLOAT_MAX);
NSString *message = @"The Internet connection appears to be offline.";

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [message boundingRectWithSize:targetSize
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:FontOpenSanWithSize(14)}
                                              context:context].size;

CGSize size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
// it will return size:width = 299 and size height 20
// => if I use this height and set for UILabel, it can display full content
// => if I use this height and set for UITextView, it can not display full content 

它非常适合 UILabel ,但是对于 UITextView 有时会计算错误.
我认为发生问题是因为 UITextView 的填充(左,右)大于 UILabel .
因此,如何计算要在 UITextView 中显示的文本的正确大小.任何帮助或建议将不胜感激.

It's work perfect for UILabel but for UITextView sometime it calculate wrong.
I think the problem happened because the padding (left, right) of UITextView is bigger than UILabel.
So how can I calculate the correct size of text for display in UITextView. Any help or suggestion would be great appreciated.

就像下面的描述图一样
具有相同的大小(300),相同的字体,相同的文本,但 UITextView 则以2行显示,而 UILabel 则以1行显示.而且我的用于计算高度的代码返回20,不足以显示两行,因此 UITextView 无法显示全部内容

Like the description image below
With the same size (300), same font, same text but the UITextView display in 2 lines, but UILabel in 1 lines. And my code for calculate height return 20, it not enough for display in 2 lines, so the UITextViewcan not display full content

我需要基于文本计算 UITextView 高度的原因,因为我的 UITextView 在弹出窗口中.弹出高度取决于 TextView 高度

The reason why I need to calculate the height of UITextView base on text because my UITextView is in a popup. And the popup height will depend on the TextView height

推荐答案

您可以尝试两种方法:

  1. 设置 textView.textContainerInset = UIEdgeInsetsZero
  2. 设置 textView.textContainer.lineFragmentPadding = 0

通过这些操作,您可以消除textView中的所有填充,并且当其宽度与标签的宽度匹配时,其高度也相同.

With these operations you can get rid of all the padding in the textView and when its width matches with the label's one the heights are also the same.

这是一个示例代码,您可以将其放置在一个空的viewController中并自己进行测试:

Here's a sample code you can place in an empty viewController and test it yourself:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSString *text = @"The internet connection appears to be offline.";
    CGFloat width = 100.f;

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, width, 300)];
    textView.font = [UIFont fontWithName:@"AvenirNext-Regular" size:12.f];
    textView.text = text;
    [self.view addSubview:textView];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 + width, 20, width, 300)];
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:@"AvenirNext-Regular" size:12.f];
    label.text = text;
    [self.view addSubview:label];

    // Getting rid of textView's padding
    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;

    // Setting height of textView to its contentSize.height
    CGRect textViewFrame = textView.frame;
    textViewFrame.size = textView.contentSize;
    textView.frame = textViewFrame;

    // Setting height of label accorting to it contents and width
    CGRect labelFrame = label.frame;
    labelFrame.size = [label sizeThatFits:CGSizeMake(width, HUGE_VALF)];
    labelFrame.size.width = width;
    label.frame = labelFrame;

    NSLog(@"Label bounds: %@", NSStringFromCGRect(label.bounds));
    NSLog(@"TextView bounds: %@", NSStringFromCGRect(textView.bounds));

    // Visualizing final effect with borders
    textView.layer.borderColor = [UIColor redColor].CGColor;
    textView.layer.borderWidth = 1.f;
    label.layer.borderColor = [UIColor greenColor].CGColor;
    label.layer.borderWidth = 1.f;
}

控制台输出:

2016-09-01 14:29:06.118 stack39268477[943:243243] Label bounds: {{0, 0}, {100, 66}}
2016-09-01 14:29:06.119 stack39268477[943:243243] TextView bounds: {{0, 0}, {100, 66}}

这篇关于如何基于文本计算TextView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆