如何在动作表样式的UIAlertController中具有多种突出显示的颜色?

时间:2018-12-06 11:54:42

标签: ios swift

我有一个动作表样式的UIAlertController,它有2个常规动作和一个cancel动作,我想为其添加不同颜色的文本,所以我将UIAlertController子类化:

class CustomAlertViewController: UIAlertController {

internal var cancelText: String?

private let fontRegular = UIFont(name: "IRANSans", size: 16)
private let fontBold = UIFont(name: "IRANSans-Bold", size: 16)

override func viewDidLoad() {
    super.viewDidLoad()

   self.view.tintColor = UIColor.black
}       


override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    self.findLabel(scanView: self.view)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.findLabel(scanView: self.view)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.findLabel(scanView: self.view)
}

func findLabel(scanView: UIView!) {
    if (scanView.subviews.count > 0) {
        for subview in scanView.subviews {
            if let label: UILabel = subview as? UILabel {

                if (self.cancelText != nil && label.text == self.cancelText!) {
                    DispatchQueue.main.async {
                        label.tintColor = UIColor.red
                        label.highlightedTextColor = UIColor.green
                        label.font = self.fontBold
                    }
                } else {
                    label.font = self.fontRegular
                    label.tintColor = UIColor(rgb: 0x2699FB)
                    label.highlightedTextColor = UIColor.black
                }
            }

            self.findLabel(scanView: subview)
        }
    }
}

} // class end

因此,现在普通标签为蓝色,取消标签为红色。但是当我选择它们时,我不希望它们以相同的颜色突出显示。这就是问题的出处,似乎标签突出显示为UIAlertController的view.tintColor。有谁知道我该怎么做?我的意思是如何为不同的标签定义自己的突出显示颜色?

1 个答案:

答案 0 :(得分:0)

Jafar Khoshtabiat,请参见下面的代码,以在操作表中显示红色文本,希望对您有所帮助。有关更多详细信息,请参见以下链接https://nshipster.com/uialertcontroller/

// UIActionSheet

let actionSheet = UIActionSheet(title: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Destroy", otherButtonTitles: "OK")
actionSheet.actionSheetStyle = .Default
actionSheet.showInView(self.view)

// MARK: UIActionSheetDelegate

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    switch buttonIndex {
        ...
    }
}

// UIAlertController

let alertController = UIAlertController(title: nil, message: "Takes the appearance of the bottom bar if specified; otherwise, same as UIActionSheetStyleDefault.", preferredStyle: .ActionSheet)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
    // ...
}
alertController.addAction(cancelAction)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
    // ...
}
alertController.addAction(OKAction)

let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in
    println(action)
}
alertController.addAction(destroyAction)

self.presentViewController(alertController, animated: true) {
    // ...
}