首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextView autoSize

UITextView autoSize
EN

Stack Overflow用户
提问于 2011-01-15 06:10:48
回答 2查看 4.3K关注 0票数 1

我认为这将是一件容易的事情,因为人们会想要经常这样做,但我已经到处搜索并尝试了不同的方法,但似乎都不起作用。

我所要做的就是创建一个包含两行文本的UITextView。

如果文本太长,分成3行,我想自动调整字体大小,直到它适合2行。

从概念上讲,我计划做一个递归函数,不断缩小文本,直到它适合两行(基于文本字段的高度),但我无法完成它。

我们非常感谢您的任何建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-18 01:24:49

如果其他人遇到这个问题,我在这里找到了答案:

http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/

代码语言:javascript
复制
//Create a string with the text we want to display.
self.ourText = @"This is your variable-length string. Assign it any way you want!";

/* This is where we define the ideal font that the Label wants to use.
   Use the font you want to use and the largest font size you want to use. */
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:28];

int i;
/* Time to calculate the needed font size.
   This for loop starts at the largest font size, and decreases by two point sizes (i=i-2)
   Until it either hits a size that will fit or hits the minimum size we want to allow (i > 10) */
for(i = 28; i > 10; i=i-2)
{
    // Set the new font size.
    font = [font fontWithSize:i];
    // You can log the size you're trying: NSLog(@"Trying size: %u", i);

    /* This step is important: We make a constraint box 
       using only the fixed WIDTH of the UILabel. The height will
       be checked later. */ 
    CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);

    // This step checks how tall the label would be with the desired font.
    CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    /* Here is where you use the height requirement!
       Set the value in the if statement to the height of your UILabel
       If the label fits into your required height, it will break the loop
       and use that font size. */
    if(labelSize.height <= 180.0f)
        break;
}
// You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i);

// Set the UILabel's font to the newly adjusted font.
msg.font = font;

// Put the text into the UILabel outlet variable.
msg.text = self.ourText;
票数 2
EN

Stack Overflow用户

发布于 2013-07-17 13:58:47

我认为这个问题有点晚了,但是由于这是google对这个查询的最高结果,一个更合适的(复制和粘贴)解决方案可能会如下

代码语言:javascript
复制
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{

if (textView.contentSize.height > textView.frame.size.height) {
  int fontIncrement = 1;
    while (textView.contentSize.height > textView.frame.size.height) {
        textView.font = [UIFont fontWithName:@"Copperplate" size:25.0 - fontIncrement];
        fontIncrement++;

    }

}
else {
    int fontIncrement = 1;
    while (textView.font.pointSize < 25.0) {
        textView.font = [UIFont fontWithName:@"Copperplate" size:8.0 + fontIncrement];
        fontIncrement++;

    }
}
return YES;

}

在上面的代码中,25是您想要为文本视图设置的maxFontSize变量。

附注:(BOOL)textViewShouldEndEditing:是uitextview的委托方法之一,在编辑完成和键盘被放弃之前调用,因此您还应该在视图控制器中适当地包含委托协议。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4696526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档