File tree 2 files changed +17
-5
lines changed
2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -4,15 +4,15 @@ import Foundation
4
4
5
5
extension NSAttributedString : RichText {
6
6
public func color( _ color: UIColor ) -> NSAttributedString {
7
- let m = self . mutableCopy ( ) as! NSMutableAttributedString
8
- let r = NSRange ( location : 0 , length : self . string . characters . count )
7
+ let m = makeMutable ( )
8
+ let r = entireString ( )
9
9
m. addAttribute ( NSForegroundColorAttributeName, value: color, range: r)
10
10
return NSAttributedString ( attributedString: m)
11
11
}
12
12
13
13
public func font( _ font: UIFont ) -> NSAttributedString {
14
- let m = self . mutableCopy ( ) as! NSMutableAttributedString
15
- let r = NSRange ( location : 0 , length : self . string . characters . count )
14
+ let m = makeMutable ( )
15
+ let r = entireString ( )
16
16
m. addAttribute ( NSFontAttributeName, value: font, range: r)
17
17
return NSAttributedString ( attributedString: m)
18
18
}
@@ -47,3 +47,13 @@ extension NSAttributedString: RichText {
47
47
return self . font ( sizedFont)
48
48
}
49
49
}
50
+
51
+ extension NSAttributedString {
52
+ fileprivate func makeMutable( ) -> NSMutableAttributedString {
53
+ return self . mutableCopy ( ) as! NSMutableAttributedString
54
+ }
55
+
56
+ fileprivate func entireString( ) -> NSRange {
57
+ return NSRange ( location: 0 , length: self . string. characters. count)
58
+ }
59
+ }
Original file line number Diff line number Diff line change @@ -18,8 +18,10 @@ extension String: RichText {
18
18
public func color( _ color: UIColor ) -> NSAttributedString {
19
19
return rich. color ( color)
20
20
}
21
+ }
21
22
22
- private var rich : NSAttributedString {
23
+ extension String {
24
+ fileprivate var rich : NSAttributedString {
23
25
return NSAttributedString ( string: self )
24
26
}
25
27
}
You can’t perform that action at this time.
0 commit comments