Guest User

Untitled

a guest
Jan 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import UIKit
  2.  
  3. @IBDesignable class YPTextBelowImageButton: UIButton {
  4.  
  5. @IBInspectable var heightRatio: CGFloat = 0.8 {
  6. didSet {
  7. self.setNeedsDisplay()
  8. }
  9. }
  10.  
  11. override init(frame: CGRect) {
  12. super.init(frame: frame)
  13. self.frame = frame
  14. self.imageView?.contentMode = .scaleAspectFit
  15. self.titleLabel?.textAlignment = .center
  16. self.setNeedsDisplay()
  17. self.setNeedsLayout()
  18. }
  19.  
  20. required init?(coder aDecoder: NSCoder) {
  21. super.init(coder: aDecoder)
  22. self.imageView?.contentMode = .scaleAspectFit
  23. self.titleLabel?.textAlignment = .center
  24. self.setNeedsDisplay()
  25. self.setNeedsLayout()
  26. }
  27.  
  28. override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
  29. return CGRect(x: 0, y: 0, width: contentRect.size.width, height: contentRect.size.height * heightRatio)
  30. }
  31.  
  32. override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
  33. return CGRect(x: 0, y: contentRect.size.height * heightRatio, width: contentRect.size.width, height: contentRect.size.height * (1 - heightRatio))
  34. }
  35.  
  36. }
Add Comment
Please, Sign In to add comment