我这里有解决方案。我创建了一个自定义的AlertThemeConfigurator,它会递归地遍历所有子视图,寻找UILabels,然后为标题、消息和不同类型的操作设置主题属性文本。请随意适当地样式化属性字符串。
class AlertThemeConfigurator {
class func configureAlertViewController(alertController : UIAlertController) {
AlertLabelConfigurator.adjustLabels(inView: alertController.view, alertController: alertController)
}
class AlertLabelConfigurator {
class func adjustLabels(inView view : UIView, alertController : UIAlertController) {
for subview in view.subviews {
if subview is UILabel {
adjustLabel((subview as! UILabel), inAlertViewController : alertController)
}
adjustLabels(inView :subview, alertController : alertController)
}
}
class func adjustLabel(label : UILabel, inAlertViewController alertController : UIAlertController) {
if label.text == alertController.title {
label.attributedText = attributedTitle(label.text!)
} else if label.text == alertController.message {
label.attributedText = attributedBody(label.text!)
}
for action in alertController.actions {
if label.text == action.title {
switch action.style {
case .Default:
label.attributedText = attributedDefaultAction(action.title!)
case .Cancel:
label.attributedText = attributedCancelAction(action.title!)
case .Destructive:
label.attributedText = attributedDestructiveAction(action.title!)
}
}
}
}
class func attributedTitle(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 20)!, NSForegroundColorAttributeName : UIColor.greenColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedBody(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 12)!, NSForegroundColorAttributeName : UIColor.orangeColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedDefaultAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.yellowColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedCancelAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.purpleColor()]
return NSAttributedString(string: title, attributes: attributes)
}
class func attributedDestructiveAction(title : String) -> NSAttributedString {
let attributes = [NSFontAttributeName:UIFont(name: "Avenir-Medium", size: 14)!, NSForegroundColorAttributeName : UIColor.redColor()]
return NSAttributedString(string: title, attributes: attributes)
}
}
}
呈现它时,请调用:
let alert = CustomAlertController(title: "Title", message:"Message" , preferredStyle: UIAlertControllerStyle.ActionSheet)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
AlertThemeConfigurator.configureAlertViewController(alert)
label.Font = UIFont(name: StyleResources.MediumFontName, size: 20)
? - Jigar Tarsariya