views:

320

answers:

2

I've seen this post: http://stackoverflow.com/questions/2035567/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 String 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.)

+1  A: 

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.

Steven Canfield
Will that follow the "\n" characters, or ignore them still?
Moshe
This is not ok if you need to specify the where the line breaks are from a file.
MihaiD
+1  A: 

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

KennyTM