質問編集履歴

2

コードを追記しました

2018/07/18 06:15

投稿

lyzmfeqpxs54
lyzmfeqpxs54

スコア237

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,45 @@
14
14
 
15
15
 
16
16
 
17
- SizeToFitメソッドで
17
+ ```Swift
18
18
 
19
19
 
20
20
 
21
+ class PaddingLabel: UILabel {
22
+
23
+
24
+
25
+ @IBInspectable var padding: UIEdgeInsets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
26
+
27
+
28
+
29
+ override func drawText(in rect: CGRect) {
30
+
31
+ let newRect = UIEdgeInsetsInsetRect(rect, padding)
32
+
33
+ super.drawText(in: newRect)
34
+
35
+ }
36
+
37
+
38
+
39
+ override func sizeThatFits(_ size: CGSize) -> CGSize {
40
+
41
+ var contentSize = super.sizeThatFits(size)
42
+
43
+ contentSize.width += padding.left + padding.right
44
+
45
+ contentSize.height += padding.top + padding.bottom
46
+
21
- ```Swift
47
+ return contentSize
48
+
49
+ }
50
+
51
+
52
+
53
+ }
54
+
55
+
22
56
 
23
57
  let contentsLabel = UILabel()
24
58
 

1

コードを追記しました

2018/07/18 06:15

投稿

lyzmfeqpxs54
lyzmfeqpxs54

スコア237

test CHANGED
File without changes
test CHANGED
@@ -22,9 +22,15 @@
22
22
 
23
23
  let contentsLabel = UILabel()
24
24
 
25
+ contentsLabel = PaddingLabel(frame: CGRect(x: 0, y: 0, width: 258.25, height: 100))
26
+
27
+ contentsLabel.font = UIFont(name:"HiraKakuProN-W3", size: 16)
28
+
29
+ contentsLabel.backgroundColor = UIColor.white
30
+
31
+ contentsLabel.numberOfLines = 0
32
+
25
33
  contentsLabel.text = "長い文章・・・・・・・・・"
26
-
27
- (中略)
28
34
 
29
35
  print(contentsLabel.frame.width) //258.25
30
36