Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UILabel sizeToFit无法正常工作。使用setNumberOfLines不会';不行。需要可变高度函数才能工作_Iphone_Xcode_Storyboard - Fatal编程技术网

Iphone UILabel sizeToFit无法正常工作。使用setNumberOfLines不会';不行。需要可变高度函数才能工作

Iphone UILabel sizeToFit无法正常工作。使用setNumberOfLines不会';不行。需要可变高度函数才能工作,iphone,xcode,storyboard,Iphone,Xcode,Storyboard,我试图通过UILabel显示可变高度的文本量 我通过结合Xcode的故事板和直接编码到实现文件中来设置一切 代码如下: CGRect labelFrame = CGRectMake(20, 20, 280, 800); descriptionLabel.frame = labelFrame; NSString *description = self.spell[@"description"]; descriptionLabel.text = description; [descriptionLa

我试图通过UILabel显示可变高度的文本量
我通过结合Xcode的故事板和直接编码到实现文件中来设置一切

代码如下:

CGRect labelFrame = CGRectMake(20, 20, 280, 800);
descriptionLabel.frame = labelFrame;
NSString *description = self.spell[@"description"];
descriptionLabel.text = description;
[descriptionLabel setNumberOfLines:0];
[descriptionLabel sizeToFit];
我尝试过更改wordwrap函数,尝试过使用很多不同的设置,但都没有效果

故事板中是否需要做一些特定的事情
“我的视图->标签”的结构重要吗

我有它,以便标签在一个视图中(占据整个屏幕)

我试着创造同样的东西,它完全适合我。(我的应用程序中没有故事板,但我认为它不会影响任何东西。)


以下是我对任何问题的解决方案,如您的问题(动态高度):


在我看来,这是确定所需高度的最安全的方法。另外,据我所知,如果sizeToFit已经足够容纳所有文本,则调用sizeToFit不会改变其大小。(不会使其变小…如果需要,只会变大,但不会添加更多行)

看起来您没有设置lineBreakMode

我从stackoverflow那里得到了一些代码,当我把它放到一个类别中时,你可以在这里了解它:

但是,如果您只是想要代码,那么它就是:

@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end
 
@implementation UILabel (EnkiExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
    if (fixedWidth < 0) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    } else {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
    }
    self.lineBreakMode = UILineBreakModeWordWrap;
    self.numberOfLines = 0;
    [self sizeToFit];
}
@end

不需要手动计算高度。对于可变高度,在调用
sizeToFit
之前,将标签的高度设置为0,如下所示

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
label.numberOfLines = 0;
label.text = @"Some long piece of text...";

[label sizeToFit];

我也有剧团,我的代码如下:

helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];

    [tagBtn.titleLabel sizeToFit];
tempBtn.titleLabel.frame.size.width
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
    [tagBtn.titleLabel sizeToFit];
你知道的UIbutton的TeacherTagBtn子类! 然后我想得到标签的宽度,如下所示:

helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];

    [tagBtn.titleLabel sizeToFit];
tempBtn.titleLabel.frame.size.width
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
    [tagBtn.titleLabel sizeToFit];
最后,我失败了。宽度为0。 所以像这样:

helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];

    [tagBtn.titleLabel sizeToFit];
tempBtn.titleLabel.frame.size.width
    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
    [tagBtn.titleLabel sizeToFit];

只添加框架,它的工作完美

我使用的是SWIFT,我面临着同样的问题。我在添加了
layoutifneedd()后修复了它。我的代码如下:

self.MyLabel.sizeToFit()
self.MyLabel.layoutIfNeeded()

看看我为iOS 7开发的多行标签的设置,这不起作用。也许在某些情况下确实如此,但在我的情况下,一切都按照你的方式设置,再加上背景色和更大的字体,它就不起作用了。它只是添加了省略号。在故事板上将标签的高度设置为0在swift中为我做到了这一点。在代码中设置帧无效。