myOriginForThisSection

myOriginForThisSection

本文介绍了换行符在UILabel中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从plist中加载一些帮助文本,并以UIScrollView中的UILabels形式显示。部分代码如下:

$ p
$ b $ p $ $ $ $ $ $ > 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'字符。我只看到字符\和n印在UILabel的任何地方,而不是我想要的换行符。



我看了这个和普遍的共识似乎是将numberOfLines设置为0,将lineBreakMode设置为有效值,并调用sizeToFit(或基于sizeWithFont设置UILabel的框架)应该做的。我似乎在上面的代码中所做的所有这些工作 - 在UILabel上将多个未知长度的字符串装入多行时,完美地工作。那么在这里可能会丢失什么?注意:所有使用的变量baseScrollView,myStringForThisSection和myOriginForThisSection在上面的代码开始执行之前被加载,并且工作正常。 p>

解决方案

UILabel不解释转义序列\\\
。您可以插入表示回车和/或换行符的真实字符。创建一个char来保存你的新行,然后插入它。

  unichar newLine ='\\\
';
NSString * singleCR = [NSString stringWithCharacters:& newLine length:1];
[myStringForThisSection insertString:singleCR atIndex:somePlaceIWantACR];

只要您的myStringForThisSection是可变的,就应该这样做。


I'm loading some help text from a plist and displaying the same in the form of UILabels housed in a UIScrollView. Portion of the code follows:

    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];

While any 'long' text is getting wrapped into multiple lines correctly, I'm unable to manually insert any line-breaks using newline '\n' characters in 'myStringForThisSection'. I only see the characters '\' and 'n' printed in the UILabel wherever I wanted the line-break, instead.

I looked this up and the general consensus seemed to be that setting numberOfLines to 0, setting the lineBreakMode to a valid value and invoking sizeToFit (or setting the frame of the UILabel based on sizeWithFont:) should do. All of which I seem to be doing in the code above - and works perfectly when fitting long strings of unknown length into multiple lines on the UILabel. So what could be missing here?

Note: All the variables used - baseScrollView, myStringForThisSection and myOriginForThisSection - were loaded before the above code began executing, and work fine.

解决方案

UILabel doesn't interpret the escape sequence \n. You can insert the real character that represents the Carriage Return and/or the Line Feed. Make a char to hold your newline and then insert it.

unichar newLine = '\n';
NSString *singleCR = [NSString stringWithCharacters:&newLine length:1];
[myStringForThisSection insertString:singleCR atIndex:somePlaceIWantACR];

As long as your myStringForThisSection is mutable, that should do it.

这篇关于换行符在UILabel中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:39