带有粗体文本的UIAlertView消息

时间:2012-03-01 10:33:04

标签: iphone uialertview

我正在尝试在“消息”中获取一些文本,以便我的UIAlertView变为粗体。像这样:

  

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

我尝试过基本的html格式化(因为“/ n”适用于新行),但这对我不起作用。是否有一种简单的方法可以使一些文本变得粗体?

当然,如果没有,我总是可以为alertview编写一个自定义的实现,但是如果有人能告诉我,如果有更简单的方法我会批评:)

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分:3)

如果您的应用在iOS 6或更低版本下运行,这一切都很好,但遗憾的是,您无法在iOS7中向您的警报视图添加子视图。 因此,如果您只需要以粗体显示消息并且您对创建自定义视图不感兴趣,则只需将消息留空即可将文本添加到标题中。

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

答案 2 :(得分:2)

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

答案 3 :(得分:2)

为了回应有耐心的回答,可以在iOS7中的UIAlertView中添加一个文本字段,只需替换

[alertView addSubview:txtField];

[alertView setValue:txtField forKeyPath:@"accessoryView"];
相关问题