【发布时间】:2010-03-31 18:48:38
【问题描述】:
我正在从 plist 加载一些帮助文本,并以 UIScrollView 中的 UILabel 的形式显示它们。部分代码如下:
UILabel *sectionDetailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(34, myOriginForThisSection, 286, 20)] autorelease];
sectionDetailLabel.backgroundColor = [UIColor clearColor];
sectionDetailLabel.numberOfLines = 0;
sectionDetailLabel.font = [UIFont systemFontOfSize:12];
sectionDetailLabel.textColor = [UIColor blackColor];
sectionDetailLabel.textAlignment = UITextAlignmentLeft;
sectionDetailLabel.lineBreakMode = UILineBreakModeWordWrap;
[baseScrollView addSubview:sectionDetailLabel];
[sectionDetailLabel setText:myStringForThisSection];
[sectionDetailLabel sizeToFit];
虽然任何“长”文本都正确地换成了多行,但我无法在“myStringForThisSection”中使用换行符“\n”手动插入任何换行符。我只看到 UILabel 中我想要换行符的地方打印了字符 '\' 和 'n'。
我查了一下,普遍的共识似乎是设置 numberOfLines 为 0,将 lineBreakMode 设置为有效值并调用 sizeToFit(或基于 sizeWithFont 设置 UILabel 的框架:)应该可以。我似乎在上面的代码中所做的所有这些 - 并且在将未知长度的长字符串拟合到 UILabel 上的多行时完美地工作。那么这里可能缺少什么?
注意:所有使用的变量——baseScrollView、myStringForThisSection 和 myOriginForThisSection——都是在上述代码开始执行之前加载的,并且工作正常。
【问题讨论】:
-
如果字符串来自外部来源,就会发生这种情况。如果它在代码中声明/创建(甚至可能在 plist 中),则换行符将被编码。但是,如果该行是空行,则换行符必须后跟空格。
标签: iphone formatting uilabel