开发者

another question about line break in UILabel

开发者 https://www.devze.com 2023-01-10 16:34 出处:网络
I\'m struggling with line break in UILabel. I\'m generating xml in vb.net and then parse it in iPhone application. xml contains text which initially contains html tags such as , so I can and need to r

I'm struggling with line break in UILabel. I'm generating xml in vb.net and then parse it in iPhone application. xml contains text which initially contains html tags such as

, so I can and need to replace these tags with something to add a linebreak in iphone

How can I do it? I tried \n , \n\r开发者_StackOverflow社区, they a not working

Any help will be appreciated

Thank you


If you read a string from an XML file, the line break \n in this string will not work in UILabel tekst. The \n is not parsed to a line break.

Here is a little trick to solve this issue:

// correct next line \n in string from XML file
NSString *myNewLineStr = @"\n";
myLabelText = [myLabelText stringByReplacingOccurrencesOfString:@"\\n" withString:myNewLineStr];

myLabel.text = myLabelText;

So you have to replace the unparsed \n part in your string by a parsed \n in a hardcoded NSString.

Here are my other label settings:

myLabel.numberOfLines = 0;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
myLabel.textAlignment = UITextAlignmentCenter;

Most important is to set numberOfLines to 0 (= unlimited nr of lines in label).

No idea why Apple has chosen to not parse \n in strings read from XML?

Hope this helps,

Al


\n should work. Make sure you set numberOfLines to 0, and lineBreakMode to something that suits you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号