2

I want to highlight some words in my UIAlertView message, either by making it bold, or, by underline.

let message = "This is normal text.\nBold Title:\n This is normal text"

let alert = UIAlertView(title: "Title", message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")

alert.show()

How could I make "Bold Title" bold (or underline) in message?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
iOS
  • 3,526
  • 3
  • 37
  • 82
  • Maybe try with something corresponding to http://stackoverflow.com/a/25260137/3151066 – Julian Aug 05 '15 at 10:52
  • Appearance works only for UI elements I guess. – iOS Aug 05 '15 at 11:01
  • perfect :) title has to be represented by some UILabel then :) I would investigate something like UILabel contained in UIAlertView and see the effects, at leat try to play around or... create your own AlertView :) – Julian Aug 05 '15 at 11:06
  • `UIAppearance` can't be used to customize `UIAlertView` - http://stackoverflow.com/questions/13928525/use-uiappearance-methods-for-customizing-uialertview – iOS Aug 05 '15 at 11:15
  • oh :( good to know. So your option is to go ahead with your own component – Julian Aug 05 '15 at 11:16

5 Answers5

3

This is what I did using accessoryView of UILabel and adding it in the Alert.Here the word YES will be in Bold in the sentence.

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title for your alert" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0, 900, 900)];
            lbl.numberOfLines=1;
            lbl.adjustsFontSizeToFitWidth = YES;
            NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:@"Please press Yes"];
            [attributedStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:20.0] range:NSMakeRange(13,3)];
            lbl.attributedText = attributedStr;
            lbl.textAlignment = NSTextAlignmentCenter;
            [alert setValue:lbl forKey:@"accessoryView"];
            [alert show];
Subin
  • 353
  • 2
  • 11
2

This is currently not possible, as the UIAlertView API does not support attributed strings, which would be the normal way you would accomplish something like this.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • that is true but I just wonder whether using appearance that could be done (?) similarly to http://stackoverflow.com/a/25260137/3151066 – Julian Aug 05 '15 at 10:51
  • I tried with `NSMutableAttributedString` also. But unsuccessful. So, there is no way to highlight? – iOS Aug 05 '15 at 10:51
  • You may potentially be able to use the UIAppearance API to change *all* the text in the alert view message, but not selectively, as the OP wants. – Andrew Ebling Aug 05 '15 at 10:55
  • that is true again but he/she didn't mention that has this kind of restrictions and I assume that it is obvious side effect of appearance :) – Julian Aug 05 '15 at 10:59
  • this is such a shame. Attributed strings would allow much more enjoyable content when displaying validation errors, for example. – Gil Sand Nov 05 '15 at 14:11
0

The UIAlertView cannot be changed. If you wish to modify it, you will need to create your own from scratch.

Please read: UIAlertView Class Reference

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Maybe you should go to UIAlertController there is support for editing.

0

Onur Keskin asked, code for Xamarin doing what OP asked. Basically Subin's answer converted:

var message = "This is normal text.\n<b>Bold Title:</b>\n This is normal text\n<b>Bold 1:</b> bold 1 text\n";

var b_open = "<b>";
var b_close = "</b>";
var boldSpots = new List<NSRange>();
var boldCount = (message.Length - message.Replace(b_open, "").Length) / b_open.Length;
for (int i = 0; i < boldCount; i++)
{
    var startBold = message.IndexOf(b_open);
    var endBold = message.IndexOf(b_close);
    message = message.Remove(startBold, b_open.Length).Remove(endBold - b_close.Length + 1, b_close.Length);
    boldSpots.Add(new NSRange(startBold, endBold - startBold - b_close.Length + 1));
}
var attrString = new NSMutableAttributedString(message);
boldSpots.ForEach(nsRange => attrString.AddAttribute(UIStringAttributeKey.Font, UIFont.FromName(ConfigurationProvider.FocoBold, 16), nsRange));

var lines = message.Length - message.Replace(@"\n", "").Length;

UILabel lbl = new UILabel(new CGRect(0, 0, 1, 1))
{
    Lines = lines,
    AdjustsFontSizeToFitWidth = true,
    AttributedText = attrString,
    TextAlignment = UITextAlignment.Center
};

var alert = new UIAlertView
{
    Title = "My Title"
};

alert.SetValueForKey(lbl, new NSString("accessoryView"));

alert.AddButton("OK");
alert.Clicked += (s, e) => {
    //OK Clicked
};
alert.Show();
Pierre
  • 8,397
  • 4
  • 64
  • 80
0

it's not with an alertView but with AlertViewController here's how you present a bold text

let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)

let attributedText = NSMutableAttributedString(string: "Bold text", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14, weight: .semibold)])
alertController.setValue(message, forKey: "attributedMessage")

controller.present(alertController, animated: true, completion: nil)
BlaShadow
  • 11,075
  • 7
  • 39
  • 60