【问题标题】:How to edit UIAlertAction text font size and color如何编辑 UIAlertAction 文本字体大小和颜色
【发布时间】:2016-06-10 09:07:17
【问题描述】:

如何编辑UIAlertAction文字大小和颜色?我已经采取了UIAlertController acoording to it how to edit the size。这是我的代码

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

现在我想要我的“注销”文本,字体大小为 22,绿色,半粗体。

【问题讨论】:

标签: ios objective-c popup uialertaction


【解决方案1】:

您可以使用

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

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

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

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

有很多开源解决方案可用。我会推荐尝试this

【讨论】:

  • 我客户的应用要求显示 LogOut 字体,大小为 17,粗细为半粗体。
  • UIAlertAction 标题标签是私有变量,不能直接访问。标签位于 3 级私有视图层次结构中。所以使用hack方式更新它会给用户带来错误(UI不会随机更新,以后很难修复)。使用更大的字体显示注销操作对应用程序有意义。有许多可用的开源解决方案。我在回答中提到了一个。
  • 我建议使用这个解决方案,因为它是 UIAlertView 控制器的类别。并在一段时间内获得良好的评价。
  • 对于 swift3 用户:let alertAction: UIAlertAction(title: "Acknowledge", style: .default, handler: nil) alertAction.setValue(UIColor.green, forKey: "titleTextColor") 如果您想更改警报控制器中按钮的文本颜色
【解决方案2】:

我写了一个扩展

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
        }

    }
}

【讨论】:

  • 法哈德干得好。简单、有用且令人惊叹。
【解决方案3】:

以下是在 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") }
    }
}

【讨论】:

    【解决方案4】:

    更改颜色非常简单。

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

    查看我的完整回答: How to change tint color of UIAlertController


    您不应该更改 UIAlertController 字体。不过还是可以的,见this answer

    【讨论】:

    • 字体大小呢??
    【解决方案5】:

    试试这个:

    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"];
    

    【讨论】:

    • @LianvanderVyver 如果图像可能太大,它会使您的应用程序崩溃。我通过在将图像设置为键“图像”之前缩放图像的大小来解决此问题
    【解决方案6】:

    使用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) {}];
    

    希望对你有帮助

    【讨论】:

    • 问题是更新 UIAlertAction 字体大小和颜色。
    【解决方案7】:

    这里给出我的答案 Swift 5。我们可以将自定义字体应用到 AlertViewAlertAddAction
    第一次创建 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")
            }
        }
    }
    
    
    
     
    

    第二次调用那些 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)
        })
    }
    

    希望它适用于所有人?

    【讨论】:

    • UIAlertAction 与键属性TitleForAction 的键值编码不兼容
    • @konradowy 是什么意思。我没有得到你。请详细解释一下。
    • @SreekanthG 你知道,在添加任何操作之前,你正在调用applyNoActionBranding。这就是为什么你没有得到任何错误。它应该得到一个没有键值编码兼容键的错误属性TitleForAction
    • 是的,我也遇到了错误,这个答案对我也不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2018-01-23
    相关资源
    最近更新 更多