-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNitroTextImpl+Attributes.swift
More file actions
84 lines (70 loc) · 2.55 KB
/
NitroTextImpl+Attributes.swift
File metadata and controls
84 lines (70 loc) · 2.55 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
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
//
// NitroTextImpl+Attributes.swift
// Pods
//
// Attribute-building helpers for NitroTextImpl (colors, paragraph style, transforms).
//
import UIKit
extension NitroTextImpl {
func makeAttributes(
for fragment: Fragment,
defaultColor: UIColor
) -> [NSAttributedString.Key: Any] {
var attrs: [NSAttributedString.Key: Any] = [:]
let font = makeFont(for: fragment, defaultPointSize: nitroTextView?.font?.pointSize)
attrs[.font] = font.value
if font.isItalic { attrs[.obliqueness] = 0.2 }
let para = makeParagraphStyle(for: fragment)
attrs[.paragraphStyle] = para
let color = resolveColor(for: fragment, defaultColor: defaultColor)
attrs[.foregroundColor] = color
return attrs
}
func makeParagraphStyle(for fragment: Fragment) -> NSMutableParagraphStyle {
let para = NSMutableParagraphStyle()
if let lineHeight = fragment.lineHeight, lineHeight > 0 {
para.minimumLineHeight = lineHeight
para.maximumLineHeight = lineHeight
}
if let align = fragment.textAlign {
switch align {
case .center: para.alignment = .center
case .right: para.alignment = .right
case .justify: para.alignment = .justified
case .left: para.alignment = .left
case .auto: para.alignment = .natural
}
} else {
para.alignment = currentTextAlignment
}
if let n = nitroTextView?.textContainer.maximumNumberOfLines {
para.lineBreakMode = effectiveLineBreakMode(forLines: n)
}
return para
}
func resolveColor(for fragment: Fragment, defaultColor: UIColor) -> UIColor {
if let value = fragment.fontColor, let parsed = ColorParser.parse(value) {
return parsed
}
return defaultColor
}
func transform(_ text: String, with fragment: Fragment) -> String {
let effective: TextTransform = {
if let ft = fragment.textTransform {
switch ft {
case .uppercase: return .uppercase
case .lowercase: return .lowercase
case .capitalize: return .capitalize
case .none: return .none
}
}
return currentTransform
}()
switch effective {
case .uppercase: return text.uppercased()
case .lowercase: return text.lowercased()
case .capitalize: return text.capitalized
case .none: return text
}
}
}