【问题标题】:Highlight words in UIAlertView? (with bold text)在 UIAlertView 中突出显示单词? (用粗体字)
【发布时间】:2015-10-28 02:25:32
【问题描述】:

我想突出显示我的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()

如何在消息中将“粗体标题”加粗(或下划线)?

【问题讨论】:

  • 外观仅适用于我猜的 UI 元素。
  • 完美 :) 标题必须由一些 UILabel 表示然后:) 我会调查 UIAlertView 中包含的 UILabel 之类的东西并查看效果,至少尝试玩一下或...创建自己的 AlertView :)
  • UIAppearance不能用于自定义UIAlertView - stackoverflow.com/questions/13928525/…
  • 哦 :( 很高兴知道。所以你的选择是继续使用你自己的组件

标签: ios swift ios8 uialertview


【解决方案1】:

这是我使用 UILabel 的附件视图并将其添加到警报中所做的。这里的单词 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];

【讨论】:

  • 哇,我很难将您的答案转换为 Xamarin C#。但这个答案对我有用。
  • @Pierre Hi Pierre,您能分享您的代码吗?
  • 好吧,我想通了。我试图在最后做 addSubview 但我应该做 alertview.SetValueForKey(label, @"accessoryView")...
【解决方案2】:

目前这是不可能的,因为 UIAlertView API 不支持属性字符串,这将是您完成此类事情的正常方式。

【讨论】:

  • 这是真的,但我只是想知道是否可以使用类似于stackoverflow.com/a/25260137/3151066的外观(?)
  • 我也试过NSMutableAttributedString。但不成功。那么,有没有办法突出显示?
  • 您可能能够使用 UIAppearance API 来更改所有警报视图消息中的文本,但不能按照 OP 的要求选择性地更改。
  • 这又是真的,但他/她没有提到有这种限制,我认为这是明显的外观副作用:)
  • 这太可惜了。例如,在显示验证错误时,带属性的字符串将提供更有趣的内容。
【解决方案3】:

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 那里支持编辑。

【讨论】:

    【解决方案4】:

    Onur Keskin 询问,Xamarin 的代码按照 OP 的要求执行。基本上苏宾的答案转换了:

    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();
    

    【讨论】:

      【解决方案5】:

      不是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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-16
        • 2014-08-07
        • 2012-11-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-19
        • 2017-01-10
        相关资源
        最近更新 更多