質問編集履歴
2
コードを追記しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,9 +6,26 @@
|
|
6
6
|
よろしくお願いいたします。
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
```Swift
|
10
10
|
|
11
|
+
class PaddingLabel: UILabel {
|
12
|
+
|
13
|
+
@IBInspectable var padding: UIEdgeInsets = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
|
14
|
+
|
15
|
+
override func drawText(in rect: CGRect) {
|
16
|
+
let newRect = UIEdgeInsetsInsetRect(rect, padding)
|
17
|
+
super.drawText(in: newRect)
|
18
|
+
}
|
19
|
+
|
20
|
+
override func sizeThatFits(_ size: CGSize) -> CGSize {
|
21
|
+
var contentSize = super.sizeThatFits(size)
|
22
|
+
contentSize.width += padding.left + padding.right
|
23
|
+
contentSize.height += padding.top + padding.bottom
|
11
|
-
|
24
|
+
return contentSize
|
25
|
+
}
|
26
|
+
|
27
|
+
}
|
28
|
+
|
12
29
|
let contentsLabel = UILabel()
|
13
30
|
contentsLabel = PaddingLabel(frame: CGRect(x: 0, y: 0, width: 258.25, height: 100))
|
14
31
|
contentsLabel.font = UIFont(name:"HiraKakuProN-W3", size: 16)
|
1
コードを追記しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,8 +10,11 @@
|
|
10
10
|
|
11
11
|
```Swift
|
12
12
|
let contentsLabel = UILabel()
|
13
|
+
contentsLabel = PaddingLabel(frame: CGRect(x: 0, y: 0, width: 258.25, height: 100))
|
14
|
+
contentsLabel.font = UIFont(name:"HiraKakuProN-W3", size: 16)
|
15
|
+
contentsLabel.backgroundColor = UIColor.white
|
16
|
+
contentsLabel.numberOfLines = 0
|
13
17
|
contentsLabel.text = "長い文章・・・・・・・・・"
|
14
|
-
(中略)
|
15
18
|
print(contentsLabel.frame.width) //258.25
|
16
19
|
contentsLabel.sizeToFit()
|
17
20
|
print(contentsLabel.frame.width) // 271.0
|