質問編集履歴

1

こちらの実装内容を追記

2017/06/27 10:11

投稿

ymkwysms
ymkwysms

スコア8

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,109 @@
9
9
 
10
10
 
11
11
  これを正常な行間で全て表示する方法はあるでしょうか?
12
+
13
+
14
+
15
+ 下記がこちらの実装内容となっております。
16
+
17
+ ```swift
18
+
19
+ @IBDesignable
20
+
21
+ class SpacingLabel: UILabel {
22
+
23
+
24
+
25
+ @IBInspectable var lineHeight: CGFloat = 0 {
26
+
27
+ didSet {
28
+
29
+ updateSpacing()
30
+
31
+ }
32
+
33
+ }
34
+
35
+ @IBInspectable var letterSpacing: CGFloat = 0 {
36
+
37
+ didSet {
38
+
39
+ updateSpacing()
40
+
41
+ }
42
+
43
+ }
44
+
45
+
46
+
47
+ override var text: String? {
48
+
49
+ didSet {
50
+
51
+ updateSpacing()
52
+
53
+ }
54
+
55
+ }
56
+
57
+
58
+
59
+ func updateSpacing() {
60
+
61
+ if let text = self.text, text.characters.count != 0 {
62
+
63
+ let attributedString = NSMutableAttributedString(string: text)
64
+
65
+ let range = NSMakeRange(0, text.characters.count)
66
+
67
+
68
+
69
+ // default attributes
70
+
71
+ attributedString.addAttribute(NSFontAttributeName, value: self.font, range: range)
72
+
73
+ attributedString.addAttribute(NSForegroundColorAttributeName, value: self.textColor, range: range)
74
+
75
+
76
+
77
+ // lineHeight attribute
78
+
79
+ if lineHeight != 0 {
80
+
81
+ let paragraphStyle = NSMutableParagraphStyle()
82
+
83
+ paragraphStyle.minimumLineHeight = lineHeight
84
+
85
+ paragraphStyle.maximumLineHeight = lineHeight
86
+
87
+ paragraphStyle.alignment = self.textAlignment // alignment from UILabel
88
+
89
+ attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range)
90
+
91
+ }
92
+
93
+ // letter Spacing
94
+
95
+ if letterSpacing != 0 {
96
+
97
+ attributedString.addAttribute(NSKernAttributeName, value: letterSpacing, range: range)
98
+
99
+ }
100
+
101
+
102
+
103
+ // show label
104
+
105
+ self.attributedText = attributedString
106
+
107
+ } else {
108
+
109
+ self.attributedText = nil
110
+
111
+ }
112
+
113
+ }
114
+
115
+ }
116
+
117
+ ```