-
Notifications
You must be signed in to change notification settings - Fork 1
/
Attribute.swift
145 lines (139 loc) · 4.77 KB
/
Attribute.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Attribute.swift
// Lipstick
//
// Created by Filip Dolnik on 18.10.16.
// Copyright © 2016 Brightify. All rights reserved.
//
import UIKit
/// Enum which represents NS attributes for NSAttributedString (like NSStrokeColorAttributeName). Each case has value and assigned name.
public enum Attribute {
case font(UIFont)
case paragraphStyle(NSParagraphStyle)
case foregroundColor(UIColor)
case backgroundColor(UIColor)
case ligature(Int)
case kern(Float)
case striketroughStyle(NSUnderlineStyle)
case underlineStyle(NSUnderlineStyle)
case strokeColor(UIColor)
case strokeWidth(Float)
case shadow(NSShadow)
case textEffect(String)
case attachment(NSTextAttachment)
case linkURL(URL)
case link(String)
case baselineOffset(Float)
case underlineColor(UIColor)
case strikethroughColor(UIColor)
case obliqueness(Float)
case expansion(Float)
case writingDirection(NSWritingDirection)
case verticalGlyphForm(Int)
public var name: String {
switch self {
case .font:
return NSFontAttributeName
case .paragraphStyle:
return NSParagraphStyleAttributeName
case .foregroundColor:
return NSForegroundColorAttributeName
case .backgroundColor:
return NSBackgroundColorAttributeName
case .ligature:
return NSLigatureAttributeName
case .kern:
return NSKernAttributeName
case .striketroughStyle:
return NSStrikethroughStyleAttributeName
case .underlineStyle:
return NSUnderlineStyleAttributeName
case .strokeColor:
return NSStrokeColorAttributeName
case .strokeWidth:
return NSStrokeWidthAttributeName
case .shadow:
return NSShadowAttributeName
case .textEffect:
return NSTextEffectAttributeName
case .attachment:
return NSAttachmentAttributeName
case .linkURL:
return NSLinkAttributeName
case .link:
return NSLinkAttributeName
case .baselineOffset:
return NSBaselineOffsetAttributeName
case .underlineColor:
return NSUnderlineColorAttributeName
case .strikethroughColor:
return NSStrikethroughColorAttributeName
case .obliqueness:
return NSObliquenessAttributeName
case .expansion:
return NSExpansionAttributeName
case .writingDirection:
return NSWritingDirectionAttributeName
case .verticalGlyphForm:
return NSVerticalGlyphFormAttributeName
}
}
public var value: AnyObject {
switch self {
case .font(let font):
return font
case .paragraphStyle(let style):
return style
case .foregroundColor(let color):
return color
case .backgroundColor(let color):
return color
case .ligature(let ligature):
return ligature as AnyObject
case .kern(let kerning):
return kerning as AnyObject
case .striketroughStyle(let style):
return style.rawValue as AnyObject
case .underlineStyle(let style):
return style.rawValue as AnyObject
case .strokeColor(let color):
return color
case .strokeWidth(let width):
return width as AnyObject
case .shadow(let shadow):
return shadow
case .textEffect(let effect):
return effect as AnyObject
case .attachment(let attachment):
return attachment
case .linkURL(let url):
return url as AnyObject
case .link(let url):
return url as AnyObject
case .baselineOffset(let offset):
return offset as AnyObject
case .underlineColor(let color):
return color
case .strikethroughColor(let color):
return color
case .obliqueness(let obliqueness):
return obliqueness as AnyObject
case .expansion(let expansion):
return expansion as AnyObject
case .writingDirection(let direction):
return direction.rawValue as AnyObject
case .verticalGlyphForm(let form):
return form as AnyObject
}
}
}
public extension Sequence where Iterator.Element == Attribute {
/// Creates dictionary from sequence of attributes by merging them together. String is name of case and AnyObject value corresponding to it.
public func toDictionary() -> [String: AnyObject] {
var attributeDictionary: [String: AnyObject] = [:]
for attribute in self {
attributeDictionary[attribute.name] = attribute.value
}
return attributeDictionary
}
}