RoundedRectLabel.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. @objc public class RoundedRectLabel : UILabel {
  23. @objc public var cornerRadius : CGFloat = 6
  24. private var bgc : UIColor?
  25. @objc public override var backgroundColor: UIColor? {
  26. get {
  27. return self.bgc
  28. }
  29. set {
  30. self.bgc = newValue
  31. super.backgroundColor = .clear
  32. }
  33. }
  34. public override func draw(_ rect: CGRect) {
  35. guard let context = UIGraphicsGetCurrentContext() else {
  36. DDLogError("Could not get current graphics context. Draw regular label")
  37. super.backgroundColor = self.bgc
  38. super.draw(rect)
  39. return
  40. }
  41. guard let text = text else {
  42. DDLogError("Text was nil. Can not draw label without text.")
  43. super.backgroundColor = self.bgc
  44. super.draw(rect)
  45. return
  46. }
  47. guard let font = font else {
  48. DDLogError("Font was nil. Can not draw label without font.")
  49. super.backgroundColor = self.bgc
  50. super.draw(rect)
  51. return
  52. }
  53. var textRect : CGRect = .null
  54. let frameSize = CGSize(width: floor(frame.size.width - cornerRadius * 0.75 * 2 + 1), height: floor(frame.size.height + 1))
  55. let textSize = text.boundingRect(with: frameSize, options: .usesLineFragmentOrigin, attributes: [
  56. NSAttributedString.Key.font: font
  57. ], context: nil).size
  58. if textAlignment == .center {
  59. textRect = CGRect(x: (frame.size.width - textSize.width) / 2, y: (frame.size.height - textSize.height) / 2, width: textSize.width, height: textSize.height)
  60. textRect = textRect.insetBy(dx: -(cornerRadius * 0.75), dy: 0)
  61. } else if textAlignment == .left {
  62. textRect = CGRect(x: 0, y: (frame.size.height - textSize.height) / 2, width: textSize.width, height: textSize.height)
  63. textRect = textRect.insetBy(dx: -(cornerRadius * 0.75), dy: 0)
  64. textRect = textRect.offsetBy(dx: cornerRadius * 0.75, dy: 0)
  65. } else if textAlignment == .right {
  66. textRect = CGRect(x: frame.size.width - textSize.width, y: (frame.size.height - textSize.height) / 2, width: textSize.width, height: textSize.height)
  67. textRect = textRect.insetBy(dx: -(cornerRadius * 0.75), dy: 0)
  68. textRect = textRect.offsetBy(dx: -(cornerRadius * 0.75), dy: 0)
  69. }
  70. let roundedRect = draw(roundedRect: textRect, context: context)
  71. super.draw(roundedRect)
  72. }
  73. public override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
  74. let rect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
  75. return rect.insetBy(dx: -(cornerRadius * 0.75), dy: 0)
  76. }
  77. public override func drawText(in rect: CGRect) {
  78. super.drawText(in: bounds.insetBy(dx: cornerRadius * 0.75, dy: 0))
  79. }
  80. func draw(roundedRect : CGRect, context : CGContext) -> CGRect {
  81. let xa = roundedRect.origin.x
  82. let xb = roundedRect.origin.x + cornerRadius
  83. let xc = roundedRect.origin.x + roundedRect.size.width - cornerRadius
  84. let xd = roundedRect.origin.x + roundedRect.size.width
  85. let ya = roundedRect.origin.y
  86. let yb = roundedRect.origin.y + cornerRadius
  87. let yc = roundedRect.origin.y + roundedRect.size.height - cornerRadius
  88. let yd = roundedRect.origin.y + roundedRect.size.height
  89. let a = CGPoint(x: CGFloat(xa), y: CGFloat(yb))
  90. let b = CGPoint(x: xa, y: ya)
  91. let b1 = CGPoint(x: xb, y: ya)
  92. let d = CGPoint(x: xc, y: ya)
  93. let e = CGPoint(x: xd, y: ya)
  94. let f = CGPoint(x: xd, y: yb)
  95. let g = CGPoint(x: xd, y: yc)
  96. let h = CGPoint(x: xd, y: yd)
  97. let i = CGPoint(x: xc, y: yd)
  98. let j = CGPoint(x: xb, y: yd)
  99. let k = CGPoint(x: xa, y: yd)
  100. let l = CGPoint(x: xa, y: yc)
  101. let m = CGPoint(x: xa, y: yb)
  102. context.beginPath()
  103. context.move(to: a)
  104. context.addArc(tangent1End: b, tangent2End: b1, radius: cornerRadius)
  105. context.addLine(to: d)
  106. context.addArc(tangent1End: e, tangent2End: f, radius: cornerRadius)
  107. context.addLine(to: g)
  108. context.addArc(tangent1End: h, tangent2End: i, radius: cornerRadius)
  109. context.addLine(to: j)
  110. context.addArc(tangent1End: k, tangent2End: l, radius: cornerRadius)
  111. context.addLine(to: m)
  112. context.closePath()
  113. guard let fillColor = self.bgc?.cgColor else {
  114. DDLogError("Could not get current background color. Draw regular label")
  115. return roundedRect
  116. }
  117. context.setFillColor(fillColor)
  118. context.fillPath()
  119. return roundedRect
  120. }
  121. }