-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUILabel+FormattedText.swift
110 lines (91 loc) · 3.67 KB
/
UILabel+FormattedText.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// UILabel+FormattedText.swift
// UILabel+FormattedText
//
// Created by João Costa on 16/09/15.
// Updated by João Costa on 13/02/19.
//
//
import Foundation
import UIKit
extension UILabel {
public var fullRange: NSRange { return NSMakeRange(0, text?.count ?? 0) }
public func range(string: String) -> NSRange? {
let range = NSString(string: forceText).range(of: string)
return range.location != NSNotFound ? range : nil
}
public func range(after string: String) -> NSRange? {
guard var range = range(string: string) else { return nil }
range.location = range.location + range.length
range.length = forceText.count - range.location
return range
}
public func range(before string: String) -> NSRange? {
guard var range = range(string: string) else { return nil }
range.length = range.location
range.location = 0
return range
}
public func range(after: String, before: String) -> NSRange? {
guard let rAfter = range(after: after),
let rBefore = range(before: before),
rAfter.location < rBefore.length
else { return nil }
return NSMakeRange(rAfter.location, rBefore.length - rAfter.location)
}
public func range(fromBeginningOf string: String) -> NSRange? {
guard var range = range(string: string) else { return nil }
range.length = forceText.count - range.location
return range
}
public func range(untilEndOf string: String) -> NSRange? {
guard var range = range(string: string) else { return nil }
range.length = range.location + range.length
range.location = 0
return range
}
public func range(fromBeginningOf beginString: String, untilEndOf endString: String) -> NSRange? {
guard let rBegin = range(fromBeginningOf: beginString),
let rEnd = range(untilEndOf: endString),
rBegin.location < rEnd.length
else { return nil }
return NSMakeRange(rBegin.location, rEnd.length - rBegin.location)
}
// MARK: Range Formatter
public func set(textColor: UIColor, range: NSRange?) {
guard let range = range else { return }
let text = mutableAttributedString
text.addAttribute(.foregroundColor, value: textColor, range: range)
attributedText = text
}
public func set(font: UIFont, range: NSRange?) {
guard let range = range else { return }
let text = mutableAttributedString
text.addAttribute(.font, value: font, range: range)
attributedText = text
}
public func set(underlineColor: UIColor, range: NSRange?, byWord: Bool = false) {
guard let range = range else { return }
let text = mutableAttributedString
var style = NSUnderlineStyle.single.rawValue
if byWord { style = style | NSUnderlineStyle.byWord.rawValue }
text.addAttribute(.underlineStyle, value: NSNumber(value: style), range: range)
text.addAttribute(.underlineColor, value: underlineColor, range: range)
attributedText = text
}
public func setTextWithoutUnderline(_ range: NSRange?) {
guard let range = range else { return }
let text = mutableAttributedString
text.removeAttribute(.underlineStyle, range: range)
attributedText = text
}
// MARK: Helpers
private var forceText: String { return text ?? "" }
private var mutableAttributedString: NSMutableAttributedString {
if let attr = attributedText {
return NSMutableAttributedString(attributedString: attr)
} else {
return NSMutableAttributedString(string: forceText)
}
}
}