質問するログイン新規登録

質問編集履歴

1

CustomLabelClassの追記

2016/05/24 01:25

投稿

label8
label8

スコア16

title CHANGED
File without changes
body CHANGED
@@ -78,4 +78,51 @@
78
78
  }
79
79
 
80
80
  }
81
+ ```
82
+
83
+ 追記:
84
+ ご指摘のあったCustomLabelClass.swiftの内容を載せてみました。
85
+ 内容は、個々でラベルのコーナーをラウンドさせるための処理になります。
86
+ ```swift
87
+ class CustomLabelClass: UILabel {
88
+
89
+ var corners: UIRectCorner = [.TopLeft, .TopRight, .BottomLeft, .BottomRight]
90
+ var radius: CGFloat = 0
91
+
92
+ var padding: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
93
+
94
+ func setPadding(pt:CGFloat, pr:CGFloat, pb:CGFloat, pl:CGFloat) {
95
+ self.padding = UIEdgeInsets(top: pt, left: pl, bottom: pb, right: pr)
96
+ }
97
+
98
+ override func drawTextInRect(rect: CGRect) {
99
+ let newRect = UIEdgeInsetsInsetRect(rect, padding)
100
+ super.drawTextInRect(newRect)
101
+ }
102
+
103
+ override func intrinsicContentSize() -> CGSize {
104
+ var intrinsicContentSize = super.intrinsicContentSize()
105
+ intrinsicContentSize.height += padding.top + padding.bottom
106
+ intrinsicContentSize.width += padding.left + padding.right
107
+ return intrinsicContentSize
108
+ }
109
+
110
+ func setCornersAndRadius(corners:UIRectCorner, radius: CGFloat) {
111
+ self.corners = corners
112
+ self.radius = radius
113
+ }
114
+
115
+ override func layoutSubviews() {
116
+ super.layoutSubviews()
117
+ let maskPath = UIBezierPath(roundedRect: self.bounds,
118
+ byRoundingCorners: self.corners,
119
+ cornerRadii: CGSize(width: self.radius, height: self.radius))
120
+
121
+ let maskLayer = CAShapeLayer()
122
+ maskLayer.frame = self.bounds
123
+ maskLayer.path = maskPath.CGPath
124
+
125
+ self.layer.mask = maskLayer
126
+ }
127
+ }
81
128
  ```