【问题标题】:Resizing a UITextView with a maximum height调整具有最大高度的 UITextView
【发布时间】:2009-08-12 08:38:21
【问题描述】:

我正在显示来自 sqlite3 数据库的一些文本,并希望 UITextView 根据文本量自动调整大小。我可以这样做:

TextLabel.scrollEnabled = YES;
TextLabel.userInteractionEnabled = YES;

[TextLabel setFrame:CGRectMake(55.10, 26.15, TextLabel.contentSize.width, 
                                             TextLabel.contentSize.height)];

这很好用。尝试设置最大高度时我很挣扎。

所以 UITextView 将根据文本调整大小,除非它达到最大高度,在这种情况下,用户将不得不使用滚动条来查看剩余的文本。

任何帮助将不胜感激

【问题讨论】:

    标签: iphone uitextview max


    【解决方案1】:

    这里有一种方法可以发现一些多行文本的所需高度:

    int myWidth = 300;
    int maxHeight = 9999;
    NSString *myString = @"lorem ipsum dolor yadda";
    UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize myStringSize = [myString sizeWithFont:myFont 
                               constrainedToSize:CGSizeMake(myWidth, maxHeight)
                                   lineBreakMode:textLabel.lineBreakMode];
    
    textLabel.frame = CGRectMake(10, 10, myWidth, myStringSize.height);
    

    我从 nevan 对上一个问题的回答中得到了这个:

    参考:Apple's docs on NSString UIKit additions

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多