开发者

Line breaks in "NSString"s returned from PList don't work

I've seen this post: NSString: newline escape in plist but I'd like to know if there's a way to pragmatically render \n line breaks.

I'm currently using:

decisionText.text = [NSString stringWithFormat: (@"%@", [replies objectAtIndex:(arc4random() % [replies count])])];

Which randomly grabs a Strin开发者_JAVA百科g from the replies array. The array was loaded from a plist like so:

replies = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"AdviceList" ofType:@"plist"]];

What am I missing? The answer can't be to manually enter line breaks. That's not programming! (Sounds like word processing to me.)


Moshe: What are you trying to do? If you're trying to render the text into a UILabel, you have to set the number of lines (setNumberOfLines:, 0 means unlimited). Then set the text(-[UILabel setText:]) and tell the label to resize itself (-[UILabel sizeToFit]). Now the label will break the lines properly to fit within the space.

Good luck.


Irrelevant to your original problem, but is still a problem:-

[NSString stringWithFormat: (@"%@", etc)];

This expression does not perform the action you really want. The extra parenthesis makes that a comma expression, where the @"%@" will be ignored, and becomes

[NSString stringWithFormat: etc];

There shouldn't be any parenthesis in the variadic call. Please use

[NSString stringWithFormat:@"%@", etc];

This can be written more efficiently as [etc description].

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜