ios - 获取物体背后的颜色

标签 ios swift colors uibutton

我想反转一个如下所示的按钮:

button to be inverted

在这种情况下,反转意味着将标题设置为蓝色,将按钮的背景设置为白色。由于我不想对其进行硬编码,因此我不能简单地将其设置为蓝色。当我将背景颜色设置为白色,并将标题颜色设置为 clearColor 时,标题当然不再可读:

button inverted incorrectly

有没有办法找出按钮后面的颜色(当前 View 的背景图像),以便我可以将标题的颜色设置为该颜色?
预先感谢:)

最佳答案

Kjuly's answer之后,您应该创建一个 UIButton 子类来执行此操作。

为了应用不同的样式,您只需为按钮触摸事件添加一些操作监听器,以便在状态更改时重新绘制按钮。

正如 Kjuly 所说,您将需要重写 drawRect 来进行自定义按钮绘制,具体取决于按钮是否被按下。

这样的事情应该可以实现你所追求的。

class CustomButton: UIButton {

    private let paragraphStyle = NSMutableParagraphStyle()
    private var titleAttributes = [String:AnyObject]()
    private var isPressed = false

    var highlightColor = UIColor.whiteColor() { // stroke & highlight color of the button
        didSet {
            setNeedsDisplay()
        }
    }
    var cornerRadius:CGFloat = 10.0 { // corner radius of button
        didSet {
            setNeedsDisplay()
        }
    }
    var strokeWidth:CGFloat = 5.0 { // stroke width of button
        didSet {
            setNeedsDisplay()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)

        addTarget(self, action: Selector("buttonWasPressed"), forControlEvents: .TouchDown)
        addTarget(self, action: Selector("buttonWasReleased"), forControlEvents: .TouchUpInside)
        addTarget(self, action: Selector("buttonWasReleased"), forControlEvents: .TouchDragExit)
        addTarget(self, action: Selector("buttonWasReleased"), forControlEvents: .TouchCancel)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func buttonWasPressed() {
        isPressed = true
        setNeedsDisplay() // set button to be redrawn
    }

    func buttonWasReleased() {
        isPressed = false
        setNeedsDisplay() // set button to be redrawn
    }

    override func drawRect(rect: CGRect) {

        let size = bounds.size
        let context = UIGraphicsGetCurrentContext()

        if isPressed { // button pressed down

            let path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius) // path of button shape for filling

            CGContextSetFillColorWithColor(context, highlightColor.CGColor) // white background of the button
            path.fill() // fill path

            CGContextSetBlendMode(context, .DestinationOut) // set blend mode for transparent label

        } else { // button not pressed

            let path = UIBezierPath(roundedRect: UIEdgeInsetsInsetRect(bounds, UIEdgeInsets(top: strokeWidth*0.5, left: strokeWidth*0.5, bottom: strokeWidth*0.5, right: strokeWidth*0.5)), cornerRadius: cornerRadius-strokeWidth*0.5) // path of button shape for stroking

            CGContextSetStrokeColorWithColor(context, highlightColor.CGColor) // set stroke color
            path.lineWidth = strokeWidth
            path.stroke()
        }

        guard let label = titleLabel else {return} // title label
        guard let text = label.text else {return} // text to draw

        // update text attributes, add any extra attributes you want transferred here.
        paragraphStyle.alignment = label.textAlignment
        titleAttributes[NSFontAttributeName] = label.font
        titleAttributes[NSParagraphStyleAttributeName] = paragraphStyle
        titleAttributes[NSForegroundColorAttributeName] = label.textColor

        let textHeight = text.sizeWithAttributes(titleAttributes).height // heigh of the text to render, with the attributes
        let renderRect = CGRect(x:0, y:(size.height-textHeight)*0.5, width:size.width, height:size.height) // rect to draw the text in

        text.drawInRect(renderRect, withAttributes: titleAttributes) // draw text
    }
}

然后,您可以通过在按钮的 titleLabel 上设置各种属性以及 strokeWidthcornerRadius 属性来使用此按钮。例如:

let button = CustomButton(frame: CGRect(x: 50, y: 50, width: 200, height: 75))

button.titleLabel?.text = "Let's go!"
button.titleLabel?.font = UIFont.systemFontOfSize(30)
button.titleLabel?.textAlignment = .Center
button.titleLabel?.textColor = UIColor.whiteColor()

button.cornerRadius = 15.0
button.strokeWidth = 5.0
view.addSubview(button)

enter image description here

关于ios - 获取物体背后的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377272/

相关文章:

ios - 设置 Root View 时的 "Application windows are expected to have a root view controller at the end of application launch"消息

c# - 如何改变色调

ios - UITableView单元格插入失败

ios - 换行时出现弹跳

python - Pandas 造型 : Conditionally change background color of column by absolute value

colors - 在ImageMagick中将TIFF图像转换为PNG时颜色错误

ios - CMVideoFormatDescriptionCreateFromH264ParameterSets 问题

iphone - 围绕 MKMapView 上的一个点

objective-c - 这是对 NSNotificationCenter 的有效使用吗

Swift,我可以用更具体的派生参数类型重写一个方法吗