亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

UIAlertController自定義字體、大小、顏色

UIAlertController自定義字體、大小、顏色

鴻蒙傳說 2019-07-04 15:15:41
UIAlertController自定義字體、大小、顏色我使用新的UIAlertController顯示警報。我有個密碼:// nil titles break alert interface on iOS 8.0, so we'll be using empty stringsUIAlertController *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];現在我想改變標題和消息的字體,顏色,大小等。做這個最好的方法是什么?
查看完整描述

3 回答

?
偶然的你

TA貢獻1841條經驗 獲得超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


查看完整回答
反對 回復 2019-07-04
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

可以通過向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 red

alertVC.view.tintColor = UIColor.greenColor()


self.presentViewController(alert, animated: true, completion: nil)


// Necessary to apply tint on iOS 9

alertVC.view.tintColor = UIColor.greenColor()


查看完整回答
反對 回復 2019-07-04
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

可以使用以下代碼更改按鈕文本的顏色:

alertC.view.tintColor = your color;

也許這個能幫你。


查看完整回答
反對 回復 2019-07-04
  • 3 回答
  • 0 關注
  • 4188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號