1

我的警报框正在使用自定义背景和白色文本,除了我似乎无法在我的操作是和否(来自 addAction 语句)上获得白色文本。当我使用属性字符串作为警报框标题和消息时,白色文本确实显示使用。

@IBAction func deletePlayer1(sender: UIButton) {
    //set player1Name to nil


    let alert = UIAlertController(title: "Delete Player", message: "Do you want to delete this player?", preferredStyle: .Alert)

    // set font size and color for message in alert box
    let attributedString = NSAttributedString(string: "Do you want to delete this player?", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString, forKeyPath: "attributedMessage")

    // set font size and color for title of alert box
    let attributedString1 = NSAttributedString(string: "Delete Player", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString1, forKeyPath: "attributedTitle")

    // set the background color and add rounded labels for the alert box
    let subView = alert.view.subviews.first! as UIView
    let contentView = subView.subviews.first! as UIView
    contentView.backgroundColor = UIColor(red: 64.0/255.0, green: 128.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    contentView.layer.cornerRadius = 10


    alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

    // set font size and color for title of action text in alert box - this is NOT working
    let attributedString2 = NSAttributedString(string: "Yes", attributes: [
    NSFontAttributeName : UIFont.systemFontOfSize(15),
    NSForegroundColorAttributeName : UIColor.whiteColor()  ])
    alert.setValue(attributedString2, forKeyPath: "attributedTitle")

    print("you press yes")



    }))

    alert.addAction(UIAlertAction(title: "No", style: .Default, handler: {(alertAction: UIAlertAction!) -> Void in

        print("you press no")

    }))

    self.presentViewController(alert, animated: true) { () -> Void in




    }
4

1 回答 1

0

您可以通过将警报的视图 tintColor 设置为白色来完成此操作。

alert.view.tintColor = .whiteColor()
于 2016-02-14T22:52:08.763 回答