如何编辑UIAlertAction文本字体大小和颜色

时间:2016-06-10 09:07:17

标签: ios objective-c popup uialertaction

如何修改UIAlertAction文字大小和颜色?我已经根据它UIAlertController如何编辑大小。这个我的代码

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];

现在我希望我的'Log Out'文本字体大小为22,绿色和半文字。

7 个答案:

答案 0 :(得分:37)

您可以使用

更新文字颜色
       UIAlertAction *myGoalAction = [UIAlertAction
                                    actionWithTitle:NSLocalizedString(@"My Title", @"My Title")
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction *action)
                                    {

                                    }];
       [myGoalAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];

没有有效的方法来更新字体大小。我建议您使用标准字体大小。

UIAlertAction标题标签是私有变量,无法直接访问。标签位于3级私有视图层次结构中。显示更大字体的注销操作对app有意义。

有许多开源解决方案可供使用。我建议您尝试this

答案 1 :(得分:3)

我写了一个扩展名

extension UIAlertController{
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        for i in self.actions {
            let attributedText = NSAttributedString(string: i.title ?? "", attributes: [NSAttributedString.Key.font : UIFont(name: "SFProText-Semibold", size: 20.0)!])

            guard let label = (i.value(forKey: "__representer") as AnyObject).value(forKey: "label") as? UILabel else { return }
            label.attributedText = attributedText
        }

    }
}

答案 2 :(得分:2)

这是在Swift中更改文本颜色的解决方案:

let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
action.setValue(UIColor.black, forKey: "titleTextColor")

或者您可以使用此扩展名:

extension UIAlertAction {
    var titleTextColor: UIColor? {
        get { return self.value(forKey: "titleTextColor") as? UIColor } 
        set { self.setValue(newValue, forKey: "titleTextColor") }
    }
}

答案 3 :(得分:1)

更改颜色非常简单。

您可以更改基础视图的tintColor,但是,由于iOS 9(https://openradar.appspot.com/22209332)中引入的已知错误,tintColor被应用程序窗口的tintColor覆盖。

请参阅我的完整答案: How to change tint color of UIAlertController


您不应该更改UIAlertController字体。但是仍然可以完成,请参阅this answer

答案 4 :(得分:0)

在这里给出我的答案 Swift 5 。我们可以将自定义字体应用于 AlertView AlertAddAction
第一个创建AlertView扩展名。内部扩展程序

import Foundation
import UIKit

extension UIAlertController {
    
    func applyBranding() {
        
        applyAlertTitleBranding()
        applyAlertMessageBranding()
    }
    
    func applyAlertTitleBranding() {
        let titleFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 18.0)!]
        let titleAttrString = NSMutableAttributedString(string: title!, attributes: titleFont as [NSAttributedString.Key : Any])
        let titleColor = UIColor(red: 34.0/255/0, green: 34.0/255/0, blue: 34.0/255/0, alpha: 1.0)
        titleAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                     value: titleColor,
                                     range: NSRange(location: 0, length: title!.count))
        setValue(titleAttrString, forKey: "attributedTitle")
    }
    
    func applyAlertMessageBranding() {
        let messageFont = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 14.0)!]
        let messageAttrString = NSMutableAttributedString(string: message!, attributes: messageFont as [NSAttributedString.Key : Any])
        let messageTitleColor = UIColor(red: 68.0/255/0, green: 68.0/255/0, blue: 68.0/255/0, alpha: 1.0)
        messageAttrString.addAttribute(NSAttributedString.Key.foregroundColor,
                                       value: messageTitleColor,
                                       range: NSRange(location: 0, length: message!.count))
        setValue(messageAttrString, forKey: "attributedMessage")
    }
    
    func applyNoActionBranding() {
        let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Medium", size: 16.0)!]
        for actionButton in actions {
            let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
            actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
        }
    }
    
    func applyYesActionBranding() {
        let font = [kCTFontAttributeName: UIFont(name: "Montserrat-Regular", size: 16.0)!]
        for actionButton in actions {
            let titleAttrString = NSMutableAttributedString(string: actionButton.title!, attributes: font as [NSAttributedString.Key : Any])
            actionButton.setValue(titleAttrString, forKey: "attributedTitleForAction")
        }
    }
}



 

2nd在需要的地方调用这些AlertView函数。例如在ViewDidLoad()

中调用
override func viewDidLoad() {
    super.viewDidLoad()

    showAlert()
}

func showAlert() {

    let alertVc = UIAlertController(title: "Some Title", message: "Some message", preferredStyle: .alert)
    
    //No action
    let noAction = UIAlertAction(title: "No", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        alertVc.dismiss(animated: true, completion: nil)
    })
    
    //Yes action
    let yesAction = UIAlertAction(title: "Yes", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.deleteFunction(address)
        alertVc.dismiss(animated: true, completion: nil)
    })
    
    alertVc.applyBranding()
    alertVc.applyNoActionBranding()
    alertVc.applyYesActionBranding()
    alertVc.addAction(noAction)
    alertVc.addAction(yesAction)
    
    alertVc.view.tintColor = .blue
    DispatchQueue.main.async(execute: {
        self.present(alertVc, animated: true, completion: nil)
    })
}

希望对所有人都有用

答案 5 :(得分:-1)

使用NSMutableAttributedString设置字体大小和颜色,使用下面的代码,

UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"" preferredStyle:UIAlertControllerStyleAlert];

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Do you wish to logout?"];

[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)];

[hogan addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,35)];

[controller setValue:hogan forKey:@"attributedTitle"];

UIAlertAction *logOut = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];

希望它有用

答案 6 :(得分:-1)

试试这个:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Do you wish to logout?" message:@"abcd" preferredStyle:UIAlertControllerStyleActionSheet];

NSMutableAttributedString *xyz = [[NSMutableAttributedString alloc] initWithString:@"pqrs"];

[xyz addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:30.0]
              range:NSMakeRange(20, 15)];
[alert setValue:xyz forKey:@"attributedTitle"];



UIAlertAction *logout = [UIAlertAction actionWithTitle:@"logout" 
                                        style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction *action){
                                                    //your code for handling this
}];
UIImage *Image = [UIImage imageNamed:@"yourImage"];
[logout setValue:accessoryImage forKey:@"image"];