UIAlertController自定义字体、大小、颜色

UIAlertController自定义字体、大小、颜色

我使用新的UIAlertController显示警报。我有个密码:

// nil titles break alert interface on iOS 8.0, so we'll be using empty strings

UIAlertController *alert = [UIAlertController alertControllerWithTitle: title == nil ? @"": title message: message preferredStyle: UIAlertControllerStyleAlert];



UIAlertAction *defaultAction = [UIAlertAction actionWithTitle: cancelButtonTitle style: UIAlertActionStyleCancel handler: nil];


[alert addAction: defaultAction];


UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;

[rootViewController presentViewController:alert animated:YES completion:nil];

现在我想改变标题和消息的字体,颜色,大小等。做这个最好的方法是什么?



鸿蒙传说
浏览 3966回答 3
3回答

偶然的你

不确定这是否针对私有api/属性,但在ios 8上使用kvc对我有用。UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" messa ge:@"" preferredStyle:UIAlertControllerStyleActionSheet];NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString :@"Presenting the great... Hulk Hogan!"];[hogan addAttribute:NSFontAttributeName               value:[UIFont systemFontOfSize:50.0]               range:NSMakeRange(24, 11)];[alertVC setValue:hogan forKey:@"attributedTitle"];UIAlertAction *button = [UIAlertAction                actionWithTitle:@"Label text"                                          style:UIAlertActionStyleDefault                                         handler:^(UIAlertAction *action){                                                     //add code to make something happen once tapped}];UIImage                                                      *accessoryImage = [UIImage imageNamed:@"someImage"];                                                     [button setValue:accessoryImage forKey:@"image"];为了记录在案,也可以使用这些私有API更改警报操作的字体。再次,它可能会让你的应用程序被拒绝,我还没有尝试提交这样的代码。let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)let action = UIAlertAction(title: "Some title", style:  .Default, handler: nil)let attributedText = NSMutableAttributedString(string: "Some title")let range = NSRange(location: 0, length: attrib  utedText.length)attributedText.addAttribute(NSKernAttributeName, value: 1.5, range: range)attributedText.addAttribute(NSFontAttributeName,   value: UIFont(name: "ProximaNova-Semibold", size: 20.0)!, range: range)alert.addAction(action)presentViewController(alert, animated: true, c  ompletion: nil)// this has to be set after presenting the alert, otherwise the internal property __representer is nilguard let label = act  ion.valueForKey("__representer")?.valueForKey("label") as? UILabel else { return }label.attributedText = attributedText对于XCode 10中的SWIFT 4.2和最后2行,现在如下所示:guard let label = (action!.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return }         label.attributedText = attributedText

catspeake

可以通过向UIAlertController应用淡色来更改按钮颜色。在IOS 9中,如果窗口颜色设置为自定义颜色,则必须在显示警报后立即应用该颜色。否则,淡色将被重置为您的自定义窗口色调颜色。// In your AppDelegate for example:window?.tintColor = UIColor.redColor()// Elsewhere in the App:let alertVC = UIAlertController(title: "Title", message: "message", preferredStyle: .Alert)alertVC.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))alertVC.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))// Works on iOS 8, but not on iOS 9// On iOS 9 the button color will be redalertVC.view.tintColor = UIColor.greenColor()self.presentViewController(alert, animated: true, completion: nil)// Necessary to apply tint on iOS 9alertVC.view.tintColor = UIColor.greenColor()

翻翻过去那场雪

可以使用以下代码更改按钮文本的颜色:alertC.view.tintColor = your color;也许这个能帮你。
打开App,查看更多内容
随时随地看视频慕课网APP