Reputation: 3616
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?
Upvotes: 2
Views: 6491
Reputation: 11693
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)
Upvotes: 0
Reputation: 9052
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();
Upvotes: 0
Reputation: 363
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];
Upvotes: 3
Reputation: 51
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.
Upvotes: 0
Reputation: 10283
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.
Upvotes: 2