UIAlertView带有粗体文本的消息

4
我试图让我的UIAlertView中的“message”文本加粗,就像这样:

UIAlertView消息:标题一些文本,新标题更多文本

我已经尝试了基本的HTML格式(因为“/n”可以换行),但是这对我没有用。有没有简单的方法可以使一些文本加粗呢?
当然,如果没有,我可以编写一个自定义实现的alertView,但如果有更简单的方法,我会感激有人告诉我:)

1
很抱歉,唯一的方法就是实现自己的警告视图,如果需要帮助,请告诉我们。 - Antonio MG
1
你需要添加一些子视图或创建自己的alertView。这个链接可能会有所帮助。 - tipycalFlow
4个回答

6

以下是你要找的代码,无需创建自定义警报

在 iOS7 上不适用

  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    UILabel *txtField = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 25.0, 260.0, 95.0)];
    [txtField setFont:[UIFont fontWithName:@"Helvetica-Bold" size:(18.0)]];
    txtField.numberOfLines = 3;
    txtField.textColor = [UIColor redColor];
    txtField.text = @"Look at me, I am a Red and Bold Label.";
    txtField.backgroundColor = [UIColor clearColor];
    [alertView addSubview:txtField];
    [alertView show];

3

如果您的应用程序运行在iOS 6或更低版本上,那么一切都很好,但不幸的是,在iOS7中您无法添加子视图到警告框。因此,如果您只需要显示粗体字消息,并且不想创建自定义视图,您可以将文本添加到标题中,而保留消息为空。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your message"
                                                    message:@""
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alertView show];

2
UILabel *alertMessage =  [alert.subviews objectAtIndex:1]; 
alertMessage.textColor = [UIColor redColor]

2
作为回应chewy的答案,可以在iOS7中向UIAlertView添加一个文本框。只需将

替换掉即可。
[alertView addSubview:txtField];

使用

[alertView setValue:txtField forKeyPath:@"accessoryView"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接