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

質問編集履歴

1

こちらの実装内容を追記

2017/06/27 10:11

投稿

ymkwysms
ymkwysms

スコア8

title CHANGED
File without changes
body CHANGED
@@ -3,4 +3,57 @@
3
3
  なぜか最後の数行だけが行間が無視されて表示されてしまいます。
4
4
  ![最後の数行のみ行間が潰れてしまう](94051c310bbc700a9a8418c243693c2c.png)
5
5
 
6
- これを正常な行間で全て表示する方法はあるでしょうか?
6
+ これを正常な行間で全て表示する方法はあるでしょうか?
7
+
8
+ 下記がこちらの実装内容となっております。
9
+ ```swift
10
+ @IBDesignable
11
+ class SpacingLabel: UILabel {
12
+
13
+ @IBInspectable var lineHeight: CGFloat = 0 {
14
+ didSet {
15
+ updateSpacing()
16
+ }
17
+ }
18
+ @IBInspectable var letterSpacing: CGFloat = 0 {
19
+ didSet {
20
+ updateSpacing()
21
+ }
22
+ }
23
+
24
+ override var text: String? {
25
+ didSet {
26
+ updateSpacing()
27
+ }
28
+ }
29
+
30
+ func updateSpacing() {
31
+ if let text = self.text, text.characters.count != 0 {
32
+ let attributedString = NSMutableAttributedString(string: text)
33
+ let range = NSMakeRange(0, text.characters.count)
34
+
35
+ // default attributes
36
+ attributedString.addAttribute(NSFontAttributeName, value: self.font, range: range)
37
+ attributedString.addAttribute(NSForegroundColorAttributeName, value: self.textColor, range: range)
38
+
39
+ // lineHeight attribute
40
+ if lineHeight != 0 {
41
+ let paragraphStyle = NSMutableParagraphStyle()
42
+ paragraphStyle.minimumLineHeight = lineHeight
43
+ paragraphStyle.maximumLineHeight = lineHeight
44
+ paragraphStyle.alignment = self.textAlignment // alignment from UILabel
45
+ attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
46
+ }
47
+ // letter Spacing
48
+ if letterSpacing != 0 {
49
+ attributedString.addAttribute(NSKernAttributeName, value: letterSpacing, range: range)
50
+ }
51
+
52
+ // show label
53
+ self.attributedText = attributedString
54
+ } else {
55
+ self.attributedText = nil
56
+ }
57
+ }
58
+ }
59
+ ```