我动态创建了一个UILabel,我将动态更新数据。
我的输出有很多行,很好 我的问题是在中间到下一行的单词中断,虽然我使用换行模式自动换行
从5小时开始用Google搜索来帮助我。
传递给UILabel的数据是正交,endonist,心理学家,心脏病学家
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];
答案 0 :(得分:0)
变化:
specialities1.lineBreakMode=UILineBreakModeClip;
到:
specialities1.lineBreakMode= UILineBreakModeWordWrap;
答案 1 :(得分: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]];
答案 2 :(得分: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];
我希望这对你有帮助......