使用多行自动收缩UILabel [英] Autoshrink on a UILabel with multiple lines

查看:86
本文介绍了使用多行自动收缩UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 UILabel 上的多行上结合使用autoshrink属性?例如,2个可用行上的大文本大小。

解决方案

这些人找到了解决方案:



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



他们的解决方案如下:

  int maxDesiredFontSize = 28; 
int minFontSize = 10;
CGFloat labelWidth = 260.0f;
CGFloat labelRequiredHeight = 180.0f;
//使用我们想要显示的文本创建一个字符串。
self.ourText = @这是你的可变长度字符串。以任何你想要的方式分配它!;

/ *这是我们定义Label想要使用的理想字体的地方。
使用您要使用的字体和要使用的最大字体大小。 * /
UIFont * font = [UIFont fontWithName:@Marker Felt尺寸:maxDesiredFontSize];

int i;
/ *计算所需字体大小的时间。
这个for循环从最大的字体大小开始,减少两个点大小(i = i-2)
直到它达到适合或达到我们想要允许的最小大小的大小( i> 10)* /
for(i = maxDesiredFontSize; i> minFontSize; i = i-2)
{
//设置新的字体大小。
font = [font fontWithSize:i];
//你可以记录你正在尝试的大小:NSLog(@试图大小:%u,i);

/ *这一步很重要:我们只使用UILabel的固定WIDTH制作约束框
。高度将在稍后检查
。 * /
CGSize constraintSize = CGSizeMake(labelWidth,MAXFLOAT);

//此步骤检查标签与所需字体的高度。
CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

/ *这是您使用身高要求的地方!
将if语句中的值设置为UILabel的高度
如果标签符合您所需的高度,它将打破循环
并使用该字体大小。 * /
if(labelSize.height< = labelRequiredHeight)
break;
}
//您可以通过输出来查看函数的大小:NSLog(@最佳大小为:%u,i);

//将UILabel的字体设置为新调整的字体。
msg.font = font;

//将文本放入UILabel出口变量中。
msg.text = self.ourText;

为了使其正常工作,必须在接口构建器中为UILabel分配IBOutlet。 / p>

IBOutlet UILabel * msg;



所有优点都是11pixel的人。


Is it possible to use the autoshrink property in conjunction on multiple lines on a UILabel? for example, the large text size possible on 2 available lines.

解决方案

These people found a solution:

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

Their solution is as follows:

int maxDesiredFontSize = 28;
int minFontSize = 10;
CGFloat labelWidth = 260.0f;
CGFloat labelRequiredHeight = 180.0f;
//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:maxDesiredFontSize];

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 = maxDesiredFontSize; i > minFontSize; 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(labelWidth, 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 <= labelRequiredHeight)
        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;

In order to get this working, a IBOutlet must be assigned in the interface builder to the UILabel.

"IBOutlet UILabel *msg;"

All the merit is of the people at 11pixel.

这篇关于使用多行自动收缩UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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