Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- @IBDesignable class YPTextBelowImageButton: UIButton {
- @IBInspectable var heightRatio: CGFloat = 0.8 {
- didSet {
- self.setNeedsDisplay()
- }
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- self.frame = frame
- self.imageView?.contentMode = .scaleAspectFit
- self.titleLabel?.textAlignment = .center
- self.setNeedsDisplay()
- self.setNeedsLayout()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- self.imageView?.contentMode = .scaleAspectFit
- self.titleLabel?.textAlignment = .center
- self.setNeedsDisplay()
- self.setNeedsLayout()
- }
- override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
- return CGRect(x: 0, y: 0, width: contentRect.size.width, height: contentRect.size.height * heightRatio)
- }
- override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
- return CGRect(x: 0, y: contentRect.size.height * heightRatio, width: contentRect.size.width, height: contentRect.size.height * (1 - heightRatio))
- }
- }
Add Comment
Please, Sign In to add comment