0

我动态创建了一个 UILabel,我将动态更新数据。

我得到了很多行的输出,这很好我的问题是在下一行中间换行虽然我使用换行模式自动换行

帮我解决这个问题,我从 5 小时开始搜索它。

传递给该 UILabel 的数据是 orthogonist,endonist,psychologist,cardioligist

CGSize constraint8 = CGSizeMake(190, 2000.0f);
CGSize size8=[temp sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint8 lineBreakMode:UILineBreakModeWordWrap];
specialities1 =[[UILabel alloc]init];
[specialities1 setFrame:CGRectMake(124,218,190, size8.height)];
specialities1.textAlignment=UITextAlignmentLeft;
specialities1.backgroundColor=[UIColor clearColor];
specialities1.numberOfLines=0;
specialities1.lineBreakMode=UILineBreakModeClip;
specialities1.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
specialities1.text=[NSString stringWithFormat:@"%@ ",temp ];
[specialities1 sizeToFit];
[testscroll addSubview:specialities1];
4

3 回答 3

0

改变:

specialities1.lineBreakMode=UILineBreakModeClip;

至 :

specialities1.lineBreakMode= UILineBreakModeWordWrap;
于 2013-02-04T11:16:36.257 回答
0

更改代码中的这两行。

根据您上面的代码,您在标签中设置了 0 行。所以改成,

specialities1.numberOfLines=size8.height/12;//(=1000, or whatever according to your requirement)
specialities1.lineBreakMode= UILineBreakModeWordWrap;

资源

获取大小

NSString *text = [dicOtherInfo  objectForKey:@"msg"];
CGSize constraint = CGSizeMake(200, 1000.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

设置大小

UILabel *lblMsg=[[UILabel alloc] initWithFrame:CGRectMake(5,15, size.width, size.height)];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
lblMsg.text = text;
lblMsg.numberOfLines = (size.height/16);
lblMsg.textAlignment=UITextAlignmentLeft;
lblMsg.backgroundColor=[UIColor clearColor];
lblMsg.textColor=[UIColor redColor];
[lblMsg setFont:[UIFont fontWithName:@"Verdana" size:12]];
于 2013-02-04T11:43:38.833 回答
0

试试我下面的方法:

-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];

[text release];
[withFont release];

return suggestedSize.height;
}

并像下面这样使用它:

    UILabel *lblAddress = [[UILabel alloc]init];
    [lblAddress setFrame:CGRectMake(110, 31, 200, 50)];        
    lblAddress.text = @"your Text ";
    lblAddress.lineBreakMode = UILineBreakModeWordWrap;
    lblAddress.numberOfLines = 0;
    lblAddress.font = [UIFont fontWithName:@"Helvetica" size:12];

    lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y, 
                             200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] ); 

    lblAddress.textColor = [UIColor darkGrayColor];

    [self.view addSubview:lblAddress];
于 2013-02-04T11:53:17.963 回答