[英]Weird behaviour on UILabel sometimes not adapting to it's content
I parse some json data and I populate some UILabels created programmatically, this is the code: 我解析一些json数据,并填充一些以编程方式创建的UILabel,这是代码:
pTitle = [[UILabel alloc] initWithFrame:CGRectMake(detailsView.frame.origin.x -10, pImage.frame.origin.y + pImage.frame.size.height + 15, detailsView.frame.size.width - 25, 30)];
pTitle.textColor = [UIColor lowBlack];
[pTitle setFont:[UIFont fontWithName:@"Arial" size:14.0]];
pTitle.lineBreakMode = NSLineBreakByWordWrapping;
pTitle.numberOfLines = 0;
[detailsView addSubview:pTitle];
The frame is fine, but after I get the callBack and set the downloaded data to the UIlabel sometimes it resizes fine but sometimes every word creates a new line because it seems that the frame gets changed. 框架很好,但是当我收到callBack并将下载的数据设置为UIlabel之后,有时它会调整大小,但是有时每个单词都会创建新行,因为看起来框架已更改。
The only thing I do after downloading the json data is to set the content to the UILabel and then call: 下载json数据后,我唯一要做的就是将内容设置为UILabel,然后调用:
[pTitle sizeToFit];
How can I fix this behaviour? 我该如何解决此问题?
EDIT: 编辑:
I found how to recreate the problem, basically if I download a product with a small name like "Test" the frame of the UILabel gets as big as the word so the next downloaded name won't be displayed larger than the word "Test". 我发现了如何重现此问题的方法,基本上,如果我下载的商品名称小如“测试”,则UILabel的框架将与单词一样大,因此下一个下载的名称将不会显示为比单词“测试”大。 。 Is it caused by the
sizeToFit
method? 它是由
sizeToFit
方法引起的吗?
I had the same problem: 我有同样的问题:
The solution that works to me was to redeclare the frame then again call the -sizeToFit
again, something like this: 对我
-sizeToFit
的解决方案是重新声明框架,然后再次调用-sizeToFit
,如下所示:
pTitle.frame = CGRectMake(detailsView.frame.origin.x -10, pImage.frame.origin.y + pImage.frame.size.height + 15, detailsView.frame.size.width - 25, 30);
[pTitle sizeToFit];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.