2017-05-26 28 views
0
let alertcontroller = UIAlertController(title: "try", message: "now", preferredStyle: .alert) 
let action = UIAlertAction(title: "Some title", style: .destructive, handler: nil) 
    let attributedText = NSMutableAttributedString(string: "New User") 

    let range = NSRange(location: 0, length: attributedText.length) 
    attributedText.addAttribute(NSKernAttributeName, value: 1.5, range: range) 
    attributedText.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 25.0), range: range) 
    action.setValue(UIColor(hex: 0xFF9B05),forKey: "titleTextColor") 
    alertcontroller.addAction(action) 
+0

哪里我在'action'中使用了'attributedText' –

+0

我认为不可能,我们可以只添加alertcontroller而不是UIAlertAction –

+0

guard let label =(Camera.value(forKey:“__representer”)as AnyObject)as? UILabel其他{返回} label.attributedText =归档文本 –

回答

0

参考形式:Refer from Here.

代码:

let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert) 
    // Background color. 
    let backView = alertController.view.subviews.last?.subviews.last 
    backView?.layer.cornerRadius = 10.0 
    backView?.backgroundColor = UIColor.yellowColor() 

    // Change Title With Color and Font: 

    let myString = "Alert Title" 
    var myMutableString = NSMutableAttributedString() 
    myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) 
    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.characters.count)) 
    alertController.setValue(myMutableString, forKey: "attributedTitle") 

    // Change Message With Color and Font 

    let message = "This is testing message." 
    var messageMutableString = NSMutableAttributedString() 
    messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!]) 
    messageMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:message.characters.count)) 
    alertController.setValue(messageMutableString, forKey: "attributedMessage") 


    // Action. 
    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil) 
    action.setValue(UIColor.orangeColor(), forKey: "titleTextColor") 
    alertController.addAction(action) 
    self.presentViewController(alertController, animated: true, completion: nil) 

OUTPUT:

enter image description here

+0

在上面的链接文字颜色正在改变提醒行动,但我不能添加自定义字体 –

+0

@AmuthaKumari - '警报行动'只具有文字'文字颜色'otherthing我们无法处理 –

+0

谢谢你的回复 –