0

我必须在 UILabel 中显示多行(如果文本很大)。下面是我的代码。我为不同的 iOS 版本使用单独的属性。请帮帮我。。

    labelLocation.numberOfLines=2;
    labelLocation.font=[UIFont systemFontOfSize:25];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6) {
        labelLocation.lineBreakMode=NSLineBreakByTruncatingTail;
        labelLocation.minimumScaleFactor=10.0/[UIFont labelFontSize];
    }else{
        labelLocation.lineBreakMode=UILineBreakModeTailTruncation;
        labelLocation.minimumFontSize=10;
    }
    labelLocation.text=@"Can we make UILabeltext in 2 lines if name is large";
4

6 回答 6

3

这两条线一起工作

labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
于 2013-08-13T11:14:35.933 回答
0

尝试这个labelLocation.numberOfLines=0;

于 2013-08-13T11:07:43.893 回答
0

将您的代码更改为此

labelLocation.numberOfLines=0;
labelLocation.font=[UIFont systemFontOfSize:40];
labelLocation.lineBreakMode=NSLineBreakModeWordWrap;
labelLocation.text=@"Can we make UILabeltext in 2 lines if name is large";
于 2013-08-13T11:44:10.533 回答
0

我想,你的标签必须很小。systemFontOfSize 25 中的两行需要高度约为 60。如果标签太小,系统不会换行。

于 2013-08-13T11:17:36.670 回答
0

你可以设置

 yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
 yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)

并检查您的插座是否设置正确。

于 2013-08-13T10:56:24.127 回答
0

我个人建议您计算显示文本所需的高度,然后将其显示到标签上……永远不要硬编码文本显示组件,例如 UITextView 和 UILable。

 NSString *str = @"This is to test the lable for the auto increment of height. This is only a test. The real data is something different.";


`UIFont * myFont = [UIFont fontWithName:@"Arial" size:12];//specify your font details here
//then calculate the required height for the above text.

CGSize lableSiZE = [str sizeWithFont:myFont constrainedToSize:CGSizeMake(240, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

//根据你从上面得到的高度初始化你的标签..你可以放任何你喜欢的宽度......

 UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lableSiZE.width, lableSiZE.height)];

myLable.text = str;
myLable.numberOfLines=0;
myLable.font=[UIFont fontWithName:@"Arial" size:12];
//myLable.backgroundColor=[UIColor redColor];
myLable.lineBreakMode = NSLineBreakByWordWrapping;
于 2013-08-13T11:45:17.283 回答