Skip to content

Commit 5bc8e39

Browse files
committed
Extracted a few methods; moved private stuff to separate extension for readability.
1 parent 357691f commit 5bc8e39

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

RichString/NSAttributedString+Rich.swift

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Foundation
44

55
extension NSAttributedString: RichText {
66
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()
99
m.addAttribute(NSForegroundColorAttributeName, value: color, range: r)
1010
return NSAttributedString(attributedString: m)
1111
}
1212

1313
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()
1616
m.addAttribute(NSFontAttributeName, value: font, range: r)
1717
return NSAttributedString(attributedString: m)
1818
}
@@ -47,3 +47,13 @@ extension NSAttributedString: RichText {
4747
return self.font(sizedFont)
4848
}
4949
}
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+
}

RichString/String+Rich.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ extension String: RichText {
1818
public func color(_ color: UIColor) -> NSAttributedString {
1919
return rich.color(color)
2020
}
21+
}
2122

22-
private var rich: NSAttributedString {
23+
extension String {
24+
fileprivate var rich: NSAttributedString {
2325
return NSAttributedString(string: self)
2426
}
2527
}

0 commit comments

Comments
 (0)