UIAlertController自定义字体不适用于iOS
UIAlertController自定义字体不起作用。
下面的代码是一个函数ShowActionSheetAsync
,显示ActionSheet
。 在这一点上,我想改变ActionSheet
的字体。 我尝试了几种方法,但效果不佳。 有没有好的解决scheme?
public Task<bool> ShowActionSheetAsync() { var source = new TaskCompletionSource<bool>(); var alert = new UIAlertController { Title = "title" }; alert.AddAction(UIAlertAction.Create( "button1", UIAlertActionStyle.Default, _ => source.SetResult(true))); alert.AddAction(UIAlertAction.Create( "cancel", UIAlertActionStyle.Cancel, _ => source.SetResult(false))); // [Block 1] var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController; ViewController.PresentViewController(alert, true, delegate { // [Block 2] }); // [Block 3] return source.Task; }
第一次尝试以下代码无法正常工作。
-
当我把代码放在
[Block 1]
或[Block 2]
- 它根本不起作用
-
当我把代码放在
[Block 3]
- 仅在第一个显示
ActionSheet
时适用。 从第二次,它不起作用
- 仅在第一个显示
UILabel.AppearanceWhenContainedIn(typeof(UIActionSheet)).Font = UIFont.FromName(StyleResources.MediumFontName, 20);
第二次尝试以下代码也无法正常工作。
-
当我把代码放在
[Block 2]
- 显示短时间的默认字体后,显示自定义字体
-
当我把代码放在
[Block 3]
- 它只在
cancel
button上工作
- 它只在
FindDescendantViews<UILabel>()
是UIView
的扩展方法,并返回适当types的所有子视图。
var labels = alert.View.FindDescendantViews<UILabel>(); foreach (var label in labels) { label.Font = UIFont.FromName(StyleResources.MediumFontName, 20); }
UIAlertController
UILabels
字体和颜色可以通过KVC使用attributedTitle
UIAlertController
键设置。 在[Block 3]
func changeAlert(alert: UIAlertController, backgroundColor: UIColor, textColor: UIColor, buttonColor: UIColor?) { let view = alert.view.firstSubview().firstSubview() view.backgroundColor = backgroundColor view.layer.cornerRadius = 10.0 // set color to UILabel font setSubviewLabelsToTextColor(textColor, view: view) // set font to alert via KVC, otherwise it'll get overwritten let titleAttributed = NSMutableAttributedString( string: alert.title!, attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(17)]) alert.setValue(titleAttributed, forKey: "attributedTitle") let messageAttributed = NSMutableAttributedString( string: alert.message!, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(13)]) alert.setValue(messageAttributed, forKey: "attributedMessage") // set the buttons to non-blue, if we have buttons if let buttonColor = buttonColor { alert.view.tintColor = buttonColor } } func setSubviewLabelsToTextColor(textColor: UIColor, view:UIView) { for subview in view.subviews { if let label = subview as? UILabel { label.textColor = textColor } else { setSubviewLabelsToTextColor(textColor, view: subview) } } }
不允许子类或更改UIAlertController的默认值。 你需要为它定制一个视图。
我在这里有解决scheme。 我创build了一个自定义的AlertThemeConfigurator,recursion地遍历所有寻找UILabels的子视图,然后为它们设置标题,消息和不同types的动作的主题的归属文本。 随意适当地设置归因string的样式。
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)