我想在UIAlertView
消息中突出显示一些字词,或者通过粗体显示,或者通过下划线。
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()
我怎样才能制作" Bold Title"消息中加粗(或下划线)?
答案 0 :(得分:3)
这就是我使用UILabel的accessoryView并在Alert中添加它所做的事情。在这句话中,单词YES将以粗体显示。
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];
答案 1 :(得分:2)
目前这是不可能的,因为UIAlertView API不支持属性字符串,这将是您完成此类事情的正常方式。
答案 2 :(得分:0)
UIAlertView无法更改。如果您想修改它,您需要从头开始创建自己的。
请阅读: 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.
也许你应该去UIAlertController,支持编辑。
答案 3 :(得分:0)
Onur Keskin问道,Xamarin的代码正在做OP所要求的。基本上Subin的答案被转换了:
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();
答案 4 :(得分:0)
它不是与alertView
一起使用,而是与AlertViewController一起使用,这是显示粗体文本的方式
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)