我必须在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";
答案 0 :(得分:3)
这两行一起工作
labelLocation.numberOfLines=0;
labelLocation.lineBreakMode = NSLineBreakByWordWrapping;
答案 1 :(得分:0)
你可以设置
yourlabelname.lineBreakMode = NSLineBreakByWordWrapping;
yourlabelname.numberOfLines = give how many lines you want for your label(e.g.2,3,etc...)
并检查您的插座是否设置正确。
答案 2 :(得分:0)
试试这个labelLocation.numberOfLines=0;
答案 3 :(得分:0)
我想,你的标签必须很小。 systemFontOfSize 25中的两行需要高度约为60。 如果标签很小,系统不会换行。
答案 4 :(得分: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";
答案 5 :(得分: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;