-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathFormattableNoticonRange.swift
More file actions
33 lines (26 loc) · 1.12 KB
/
FormattableNoticonRange.swift
File metadata and controls
33 lines (26 loc) · 1.12 KB
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
import Foundation
/// This class is used as part of the Notification Formattable Content system.
/// It inserts the given icon into an attributed string at the given range.
///
public class FormattableNoticonRange: FormattableContentRange {
public var kind: FormattableRangeKind = .noticon
public var range: NSRange
public let value: String
private var noticon: String {
return value + " "
}
public init(value: String, range: NSRange) {
self.value = value
self.range = range
}
public func apply(_ styles: FormattableContentStyles, to string: NSMutableAttributedString, withShift shift: Int) -> FormattableContentRange.Shift {
let shiftedRange = rangeShifted(by: shift)
insertIcon(to: string, at: shiftedRange)
let longerRange = NSMakeRange(shiftedRange.location, shiftedRange.length + noticon.utf16.count)
apply(styles, to: string, at: longerRange)
return noticon.utf16.count
}
func insertIcon(to string: NSMutableAttributedString, at shiftedRange: NSRange) {
string.replaceCharacters(in: shiftedRange, with: noticon)
}
}