我想反转一个如下所示的按钮:
在这种情况下,反转意味着将标题设置为蓝色,将按钮的背景设置为白色。由于我不想对其进行硬编码,因此不能简单地将其设置为蓝色。当然,当我将背景色设置为白色,而标题的颜色设置为clearColor
时,标题不再可读:
有没有办法找出按钮后面的颜色(当前是视图的背景图像),因此我可以将标题的颜色设置为该颜色?
提前致谢 :)
最佳答案
在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
上设置各种属性以及strokeWidth
和cornerRadius
属性来使用此按钮。例如: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)
关于ios - 在物体后面获得色彩,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35377272/