为什么我不能改变我的UILabel的框架? [英] Why can't I change the frame of my UILabel?

查看:132
本文介绍了为什么我不能改变我的UILabel的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了两天来修改 UILabel 的框架,其中
是荒谬的...... UILabel 是一个IBOutlet,但这不是
为什么它不起作用的原因:我试图以编程方式创建 UILabel
它仍然无法正常工作。我是这样做的:

I have been trying for two days to modify the frame of a UILabel, which is ridiculous... The UILabel is an IBOutlet, but that is not the reason why it's not working: I tried to create a UILabel programatically and it still didn't work. Here is how I do it:

self.descriptionLabel.text = description;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;

CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
                                         constrainedToSize:CGSizeMake(self.descriptionLabel.frame.size.width, FLT_MAX)
                                             lineBreakMode:self.descriptionLabel.lineBreakMode];
CGFloat frameHeight = textSize.height;
CGRect frame = self.descriptionLabel.frame;
frame.size.height = frameHeight;
self.descriptionLabel.frame = frame;

CGRect bounds = self.descriptionLabel.frame;
bounds.origin.x = self.descriptionLabel.frame.origin.x + 10.0;
bounds.size.width = self.descriptionLabel.frame.size.width - 20.0;
self.descriptionLabel.bounds = bounds;
self.descriptionLabel.numberOfLines = 0;

我已经在IRC上询问了他们告诉我没有理由为
框架不会改变...

I already asked on IRC and they told me that there is no reason why the frame would not be changed...

我还试图用 CGRectMake 创建一个框架并给它任意
值,但是没有做到这一点..

I also tried to create a frame with CGRectMake and give it arbitrary value but that didn't do the trick either..

有人知道可能出现什么问题吗?

Does anyone know what might be the problem please?

编辑1:我在之前和之后记录了帧,结果很奇怪:

Edit 1: I logged the frame before and after, and I got odd results:

Before: 0.000000 0.000000 0.000000 0.000000
After: 0.000000 0.000000 0.000000 33525.000000

之后的高度我设置框架是唯一有意义的值(我在标签上有很多文字)。

The height after I set the frame is the only value that makes sense (I have a lot of text in the label on purpose).

编辑2:我按如下方式更改了代码,日志显示框架
已更改,但在模拟器上它没有更改;我通过在 UILabel 的图层添加红色
边框来证明这一点。

Edit 2: I changed the code as follows, the log says that the frame changed but on the simulator it didn't change; I proved it by adding red borders to the layer of the UILabel.

self.descriptionLabel.text = description;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;

CGSize textSize = [self.descriptionLabel.text sizeWithFont:[UIFont systemFontOfSize:12.0]
                                         constrainedToSize:CGSizeMake(self.view.frame.size.width, FLT_MAX)
                                             lineBreakMode:self.descriptionLabel.lineBreakMode];
CGFloat frameHeight = textSize.height;
CGRect frame = CGRectMake(0.0, 300.0, self.view.frame.size.width, frameHeight);
self.descriptionLabel.frame = frame;

DDLogInfo(@"After: %f %f %f %f", self.descriptionLabel.frame.origin.x, self.descriptionLabel.frame.origin.y, self.descriptionLabel.frame.size.width, self.descriptionLabel.frame.size.height);
// After: 0.000000 300.000000 320.000000 585.000000

self.descriptionLabel.numberOfLines = 0;
self.descriptionLabel.layer.borderColor = [UIColor redColor].CGColor;
self.descriptionLabel.layer.borderWidth = 2;

推荐答案

如果您使用的是自动布局,则不应进行任何帧设置 - - 关闭自动布局,或使用约束来更改标签的大小。最简单的方法是在IB中给你的标签一个高度约束(如果你想要一个恒定的宽度也宽度),并为它做一个IBOutlet。然后,在代码中,根据从sizeWithFont得到的值更改该约束的常量值:constrainedToSize:lineBreakMode:。

If you're using auto layout, you shouldn't be doing any setting of frames -- either turn off auto layout, or use constraints to change the size of your label. The easiest way to do this is to give your label a height constraint (and width too, if you want a constant width) in IB, and make an IBOutlet to it. Then, in code, change the constant value of that constraint based on the value you get from sizeWithFont:constrainedToSize:lineBreakMode:.

这篇关于为什么我不能改变我的UILabel的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆