更改 UIAlertController 的标题字体大小 [英] Change UIAlertController's title fontsize

查看:57
本文介绍了更改 UIAlertController 的标题字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 UIAlertController 中的标题 fontSize,但我无法管理如何将我的 NSMutableAttributedString 设置为title-属性.

I'm trying to change the title fontSize in an UIAlertController, but I can't manage how to set my NSMutableAttributedString to the title-property.

因此,我一直在使用以下代码创建 NSMutableAttributedString:

So for I've been creating the NSMutableAttributedString with the following code:

let title = NSMutableAttributedString(string: user.fullName)
let range = NSRange(location: 0, length: title.length)
title.addAttribute(NSAttributedStringKey.font, value: UIFont.TextStyle.largeTitle, range: range)

现在对我来说棘手的部分是如何弄清楚如何为 UIAlertController 设置新标题,因为它需要一个 String? 值.

Now the tricky part for me is how to figure out how to set the new title to the UIAlertController, because it's expecting a String? value.

我环顾四周,发现在呈现 UIAlertController 时,我可能应该在完成块中创建一个 UILabel.但是如何使用我自己的自定义 UILabel 覆盖 UIAlertController 中的 title 属性?

I looked around and found out that I should probably create a UILabel within the completion block when presenting the UIAlertController. But how do I override the title-property in the UIAlertController with my own custom UILabel?

present(myUIAlertController, animated: true) {
    // Creating the UILabel depending on string length
    // Set the NSMutableAttributedString value to the custom UILabel and override title property.
}

或者也许有更简单的方法来解决这个问题?

Or maybe there's even an easier way to solve this?

推荐答案

您可以使用此代码为 UIAlertController 的标题和消息添加属性.您可以根据需要进行自定义.您可以在图像中看到结果.我不确定你能不能把它放在 Appstore 上.

You can make title and message of UIAlertController attributed by using this code. You can customize as per your need. You can see the result in the image. I am not sure you can put it on Appstore.

func showAlert() {
  let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)        
  let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.black]
  let titleString = NSAttributedString(string: "Name Last name", attributes: titleAttributes)     
  let messageAttributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.red]
  let messageString = NSAttributedString(string: "Company name", attributes: messageAttributes)
  alert.setValue(titleString, forKey: "attributedTitle")
  alert.setValue(messageString, forKey: "attributedMessage")
  let labelAction = UIAlertAction(title: "Label", style: .default, handler: nil)
  let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  alert.addAction(labelAction)
  alert.addAction(deleteAction)
  alert.addAction(cancelAction)
  self.navigationController?.present(alert, animated: true, completion: nil)

}

这篇关于更改 UIAlertController 的标题字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆