19

如何编辑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) {}];

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

4

7 回答 7

46

您可以使用更新文本颜色

       UIAlertAction *myGoalAction = [UIAlertAction
                                    actionWithTitle:NSLocalizedString(@"My Title", @"My Title")
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction *action)
                                    {

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

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

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

有许多可用的开源解决方案。我建议尝试一下

于 2016-06-10T12:01:19.907 回答
4

我写了一个扩展

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
        }

    }
}
于 2020-03-18T04:23:48.350 回答
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") }
    }
}
于 2020-01-24T16:13:44.330 回答
1

更改颜色非常简单。

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

请参阅我的完整答案:如何更改 UIAlertController 的色调颜色


你不应该改变UIAlertController字体。但是它仍然可以完成,请参阅此答案

于 2016-06-10T09:13:09.607 回答
-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"];
于 2016-06-10T09:25:01.790 回答
-2

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

希望它有帮助

于 2016-06-10T09:20:51.623 回答
-2

这是给我的答案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)
    })
}

希望它适用于所有人

于 2020-09-14T18:01:51.637 回答