开发者

Best way to display formatted NSString? Failed on UILabel

开发者 https://www.devze.com 2023-03-25 17:27 出处:网络
What is the best way to display a formatted NSString? I\'m trying to format the choices for a multiple choice question on an app.

What is the best way to display a formatted NSString? I'm trying to format the choices for a multiple choice question on an app.

choices1 = [NSString stringWithFormat:@"\n\n Blue \n\n"
            "Yellow \n\n"
            "Red \n\n"
            "Orange \n\n"];

I want to display that on the iPhone screen. I tried using UILabel but it doesn't recognize the new line characters. (It prints the words though).

should look like this:

Blue

Yellow

开发者_JS百科

Red

Orange

Thoughts? Thanks.


UILabel defaults to single-line display. If you want multiple lines, set numberOfLines to 0, for unlimited lines, or to whatever number of lines you want to cap the label at.


Change some properties of UILabel like this :

    NSString *str = [NSString stringWithFormat:@"\n\n Blue \n\n"
                 "Yellow \n\n"
                 "Red \n\n"
                 "Orange \n\n"];

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 250)];
    myLabel.numberOfLines = 10;
    myLabel.text = str;
    [self.view addSubview:myLabel];

Enjoy coding..


NSString *str = [NSString stringWithFormat:@"Blue
                                             \n Yellow
                                             \n Red
                                             \n Orange"];

Set numberOfLines = 4 and height of UILabel = 300(You can check if string fits in this height. If not increase the height).


Rather than spacing your choices with C syntax, you'd be better off to use 4 UILabels and just position the labels on screen exactly where you'd like them. Then you don't have to be concerned with spacing them all apart like that. It's also much easier to modify and see what you're doing when you're designing your interface graphically.

0

精彩评论

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