forked from SwiftKickMobile/SwiftMessages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.swift
58 lines (51 loc) · 2.36 KB
/
Utils.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
//
// Utils.swift
// Demo
//
// Created by Timothy Moose on 8/25/17.
// Copyright © 2017 SwiftKick Mobile. All rights reserved.
//
import UIKit
extension UILabel {
func configureBodyTextStyle() {
let bodyStyle = NSMutableParagraphStyle()
bodyStyle.lineSpacing = 5.0
attributedText = NSAttributedString(string: text ?? "", attributes: [NSAttributedString.Key.paragraphStyle : bodyStyle])
}
func configureCodeStyle(on substring: String?) {
var attributes: [NSAttributedString.Key : Any] = [:]
let codeFont = UIFont(name: "CourierNewPSMT", size: font.pointSize)!
attributes[NSAttributedString.Key.font] = codeFont
attributes[NSAttributedString.Key.backgroundColor] = UIColor(white: 0.96, alpha: 1)
attributedText = attributedText?.setAttributes(attributes: attributes, onSubstring: substring)
}
}
extension NSAttributedString {
public func setAttributes(attributes: [NSAttributedString.Key : Any], onSubstring substring: String?) -> NSAttributedString {
let mutableSelf = NSMutableAttributedString(attributedString: self)
if let substring = substring {
var range = NSRange()
repeat {
let length = mutableSelf.length
let start = range.location + range.length
let remainingLength = length - start
let remainingRange = NSRange(location: start, length: remainingLength)
range = (mutableSelf.string as NSString).range(of: substring, options: .caseInsensitive, range: remainingRange)
NSAttributedString.set(attributes: attributes, in: range, of: mutableSelf)
} while range.length > 0
} else {
let range = NSRange(location: 0, length: mutableSelf.length)
NSAttributedString.set(attributes: attributes, in: range, of: mutableSelf)
}
return mutableSelf
}
private static func set(attributes newAttributes: [NSAttributedString.Key : Any], in range: NSRange, of mutableString: NSMutableAttributedString) {
if range.length > 0 {
var attributes = mutableString.attributes(at: range.location, effectiveRange: nil)
for (key, value) in newAttributes {
attributes.updateValue(value, forKey: key)
}
mutableString.setAttributes(attributes, range: range)
}
}
}