Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView message with bold text

I'm trying to get some of the text in "message" for my UIAlertView to be bold. Like this:

UIAlertView message: Header some text, new header some more text

I've tried with basic html formatting (since "/n" works for new line), but that doesn't work for me. Is there an easy way to get some of the text bold?

Of course, if there isn't i can always write a customized implementation for an alertview, but if there's an easier way i would appriciate if someone could tell me :)

like image 632
Madoc Avatar asked Dec 13 '22 04:12

Madoc


2 Answers

Here is the code you are looking for, without creating a custom alert

DOES NOT WORK ON 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];
like image 116
chewy Avatar answered Jan 02 '23 08:01

chewy


It's all good if your app running under iOS 6 or lower, but unfortunately you can't add subviews to your alert view in iOS7. So if you need only to show message with bold font and you're not interested in creating custom views, you can simply add your text to title, when message leave empty.

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your message"
                                                    message:@""
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alertView show];
like image 30
Oleksii Kalentiev Avatar answered Jan 02 '23 10:01

Oleksii Kalentiev