swift - 更改 UIAlertController 的标题字体大小
问题描述
我正在尝试更改 中的标题fontSize
,UIAlertController
但我无法管理如何将我NSMutableAttributedString
的设置为title
-property。
因此,我一直在NSMutableAttributedString
使用以下代码创建:
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?
值。
我环顾四周,发现UILabel
在展示UIAlertController
. 但是如何用我自己的自定义覆盖title
-property ?UIAlertController
UILabel
present(myUIAlertController, animated: true) {
// Creating the UILabel depending on string length
// Set the NSMutableAttributedString value to the custom UILabel and override title property.
}
或者也许有更简单的方法来解决这个问题?
我的目标是如下图所示:
解决方案
您可以UIAlertController
使用此代码制作属性的标题和消息。您可以根据自己的需要进行定制。您可以在图像中看到结果。我不确定你可以把它放在 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)
}
推荐阅读
- file - Windows 的 .testcaferc.json 文件的位置
- python - 树视图中已存在项目“第 7 层”:在 Maya 中
- laravel - 如果用户在 laravel 6 中通过身份验证,则显示不同的 http 错误页面
- reactjs - Toogle 按钮未正确更新状态
- reactjs - 将 Gatsby JS 用于简单的 CRUD Web 应用程序:是否值得推荐?
- typescript - 当类型参数同时用作属性类型和构造函数参数类型时,如何修复不兼容的类型?
- r - 导入的带日期的 Excel 文件,以数字形式出现,试图返回日期格式
- unity3d - unity3D中3D模型中间的对称线
- c# - WPF 自定义面板子初始化
- ios - 具有两名成员的团队中的个人 Apple 开发者帐户