没有太多的代码要分享,只是一个问题。如果高度超过 8191 像素,为什么我的标签不可见?
你可能认为这太过分了,为什么我想要这么长的标签……它是动态的,所以你永远不知道。
所以事情是这样的:我创建了我的 UIScrollView 并开始在 init 中添加标签,其中 5 个。我设置了文本,一切顺利。我有一个方法可以获取 5 个标签,使用 NSString sizeWithFont:constrainedToSize:lineBreakMode:
获取大小,重新排列它们并重置 UIScrollView 的 contentHeight。都好。事实是,当标签的高度正好超过 8191 像素(宽度超过 300)时,它就会消失,不可见,噗!走了。
如果我不能让它工作,我想我可以将文本分成多个部分并创建额外的 UILabels,但我想避免这种情况。
有任何想法吗?
这是一些虚拟代码,易于遵循
NSError *er = nil;
// this file can be found here:
// https://gist.github.com/3167635
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"lorem.txt"];
NSString *labelText = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&er];
if(er != nil)
NSLog(@"Error: %@", er);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.bounds];
[scrollView setBackgroundColor:[UIColor whiteColor]];
UILabel *label = [[UILabel alloc] init];
[label setBackgroundColor:[UIColor whiteColor]];
[label setNumberOfLines:0];
[label setText:labelText];
CGSize size = [labelText sizeWithFont:label.font constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:UILineBreakModeCharacterWrap];
NSLog(@"Size: %@", NSStringFromCGSize(size));
CGRect labelFrame;
labelFrame.origin = CGPointMake(10, 0);
labelFrame.size = size;
[label setFrame:labelFrame];
[scrollView setContentSize:size];
[scrollView addSubview:label];
[[self view] addSubview:scrollView];
虚拟文本很大,使标签不可见。
最佳答案
我建议使用 sizeToFit
属性而不是自己设置高度
由于此属性将根据您的文本设置标签的高度,因此您不必费力设置高度
或者你可以改用这条线
CGSize maximumLabelSize = CGSizeMake(headerView.frame.size.height,over 8191 );
关于objective-c - UILabel 有最大高度吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11623317/