显示格式化 NSString 的最佳方式? UILabel 失败

发布于 2024-11-27 15:30:49 字数 352 浏览 1 评论 0原文

显示格式化 NSString 的最佳方式是什么?我正在尝试格式化应用程序上多项选择题的选项。

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

我想在 iPhone 屏幕上显示它。我尝试使用 UILabel 但它无法识别新行字符。 (虽然它打印了这些词)。

应该是这样的:

蓝色

黄色

红色

橙色

想法?谢谢。

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

Red

Orange

Thoughts? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾听心声的旋律 2024-12-04 15:30:49

UILabel默认为单行显示。如果您需要多行,请将 numberOfLines 设置为 0(无限行),或者设置为您想要限制标签的任意行数。

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.

陪你到最终 2024-12-04 15:30:49

像这样更改 UILabel 的一些属性:

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

享受编码..

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

北斗星光 2024-12-04 15:30:49
NSString *str = [NSString stringWithFormat:@"Blue
                                             \n Yellow
                                             \n Red
                                             \n Orange"];

设置numberOfLines = 4UILabel = 300的高度(您可以检查字符串是否适合这个高度。如果不适合增加高度)。

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

玻璃人 2024-12-04 15:30:49

您最好使用 4 个 UILabels,并将标签准确地放置在屏幕上您想要的位置,而不是使用 C 语法来分隔您的选择。那么你就不必担心像这样将它们分开。当您以图形方式设计界面时,修改和查看正在执行的操作也更加容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文