简体   繁体   中英

Swift UIButton Tint Text When Highlighted

In my app, I have a button with a text title next to a button that's an image. I want the color scheme of both buttons to match. I create the button with the image like this:

let button1 = UIButton()
button1.setImage(
UIImage(named: "button1")?.withRenderingMode(.alwaysTemplate), for: .normal)
button1.tintColor = UIColor.green

This creates the effect that I want on both buttons, ie the button is green, then when it's highlighted it gets tinted to a darker, black-ish green. I tried creating the text button the same way:

let button2 = UIButton()
button2.setTitle("button2", for: .normal)
button2.tintColor = UIColor.green

But, in this case, setting the tint color doesn't change the color of the button's title/text (it remains white even when highlighted). My solution to this is as follows:

let button2 = UIButton()
button2.setTitle("button2", for: .normal)
button2.setTitleColor(UIColor.green, for: .normal)
button2.setTitleColor(UIColor(red: 0x23 / 255.0,
                              green: 0x34 / 255.0,
                              blue: 0x16 / 255.0,
                              alpha: 1.0), for: .highlighted)

Essentially, I've estimated the color that the image gets tinted to when it's highligted and set the text color to match. This works fine, but it bothers me that I only have an approximation; ideally, I would want the system to tint the text color for me when the button is highlighted in the same way that it tints the image. I get that this is a really small problem and that fixing it probably won't noticeably improve the app, but I'd still like to know if there's a way to tint a button with a text title "automatically" (as opposed to hardcoding the tint).

Tint color is property of UIView, which doesn't have a state. State is property of UIControl(Button's parent class). Means that you cannot change the tint on the bases of button's state. You can only change properties mentioned seen in this screen shot on the basis of button's state by default.

在此处输入图片说明

Also the

darker, black-ish green

color your getting that the default behaviour of button to change background color to show highlighted state

Solution : CustomButton

Create a custom UIButton

class MyButton : UIButton {
    override var isHighlighted: Bool{
        didSet {
            tintColor = isHighlighted ? UIColor.green : UIColor.red
            // do additional work here according to your need
        }
    }

    override var isSelected: Bool {
        didSet {
            // do changes according to you need
        }
    }
}

You can also set the properties mentioned in above image programmatically.

button.setTitleColor(UIColor.green, for: .normal)
button.setTitleColor(UIColor.red, for: .highlighted)
button.setBackgroundImage(yourBackgroundImage, for: .normal)
let button2 = UIButton()
button2.addTarget(self, action: #selector(self.pressed), for: [.touchDown])
button2.addTarget(self, action: #selector(self.released), for: [.touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel])


func pressed() {
   // set colour
}

func released() {
   // set colour
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM