forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSDictionaryDTCoreText.m
255 lines (187 loc) · 9.21 KB
/
NSDictionaryDTCoreText.m
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// NSDictionaryDTCoreText.m
// DTCoreText
//
// Created by Oliver Drobnik on 02.10.13.
// Copyright (c) 2013 Drobnik.com. All rights reserved.
//
#import "DTCompatibility.h"
#import "NSDictionary+DTCoreText.h"
#import "DTCoreTextTestCase.h"
@interface NSDictionaryDTCoreText : DTCoreTextTestCase
@end
@implementation NSDictionaryDTCoreText
- (void)testBold
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<b>bold</b>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isBold], @"Attributes should be bold");
}
- (void)testItalic
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<i>italic</i>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isItalic], @"Attributes should be italic");
}
- (void)testUnderline
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<u>underline</u>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isUnderline], @"Attributes should be underlined");
}
- (void)testNSUnderline
{
if (!DTCoreTextModernAttributesPossible())
{
return;
}
NSDictionary *buildAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSUnderlineStyleAttributeName];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:buildAttributes];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isUnderline], @"Attributes should be underlined");
}
- (void)testStrikethrough
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<del>strikethrough</del>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isStrikethrough], @"Attributes should be strikethrough");
}
- (void)testNSStrikethrough
{
if (!DTCoreTextModernAttributesPossible())
{
return;
}
NSDictionary *buildAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSStrikethroughStyleAttributeName];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:buildAttributes];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes isStrikethrough], @"Attributes should be strikethrough");
}
- (void)testHeaderLevel
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<h3>header</h3>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes headerLevel]==3, @"Header level should be 3");
}
- (void)testHasAttachment
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<img src=\"Oliver.jpg\">" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertTrue([attributes hasAttachment], @"There should be a text attachment");
}
- (void)testParagraphStyle
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<p>Paragraph</p>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTCoreTextParagraphStyle *paragraphStyle = [attributes paragraphStyle];
XCTAssertNotNil(paragraphStyle, @"There should be a paragraph style");
XCTAssertTrue([paragraphStyle isKindOfClass:[DTCoreTextParagraphStyle class]], @"Should be a DTCoreTextParagraphStyle");
}
- (void)testParagraphStyleNil
{
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertNil([attributes paragraphStyle], @"There should be no paragraph style");
}
- (void)testNSParagraphStyle
{
if (!DTCoreTextModernAttributesPossible())
{
return;
}
NSMutableParagraphStyle *nsParagraphStyle = [[NSMutableParagraphStyle alloc] init];
NSDictionary *buildAttributes = [NSDictionary dictionaryWithObject:nsParagraphStyle forKey:NSParagraphStyleAttributeName];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:buildAttributes];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTCoreTextParagraphStyle *paragraphStyle = [attributes paragraphStyle];
XCTAssertNotNil(paragraphStyle, @"There should be a paragraph style");
XCTAssertTrue([paragraphStyle isKindOfClass:[DTCoreTextParagraphStyle class]], @"Should be a DTCoreTextParagraphStyle");
}
- (void)testFontDescriptor
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<p>Paragraph</p>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertNotNil([attributes fontDescriptor], @"There should be a font descriptor");
}
// tests if the fontDescriptor convenience method of NSDictionary always returns something correct
- (void)testDirectFontDescriptor
{
#if TARGET_OS_IPHONE
UIFont *font = [UIFont fontWithName:@"Courier" size:12];
NSDictionary *attributes = @{NSFontAttributeName: font};
#else
NSFont *font = [NSFont fontWithName:@"Courier" size:12];
NSDictionary *attributes = @{NSFontAttributeName: font};
#endif
DTCoreTextFontDescriptor *fontDescriptor = [attributes fontDescriptor];
XCTAssertNotNil(fontDescriptor, @"There should be a font descriptor");
XCTAssertEqualObjects(fontDescriptor.fontFamily, @"Courier", @"Font Family should be 'Courier'");
}
- (void)testFontDescriptorNil
{
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
XCTAssertNil([attributes fontDescriptor], @"There should be no font descriptor");
}
- (void)testColorDefaults
{
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTColor *color = [attributes foregroundColor];
NSString *hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"000000"], @"Default Color should be black");
color = [attributes backgroundColor];
XCTAssertNil(color, @"Background Color should be nil");
}
- (void)testValidColors
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<span style=\"color:red;background-color:blue;\">Paragraph</span>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTColor *color = [attributes foregroundColor];
NSString *hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"ff0000"], @"Default Color should be red");
color = [attributes backgroundColor];
hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"0000ff"], @"Default Color should be blue");
}
- (void)testNSValidColors
{
if (!DTCoreTextModernAttributesPossible())
{
return;
}
NSDictionary *buildAttributes = [NSDictionary dictionaryWithObjectsAndKeys:DTColorCreateWithHTMLName(@"red"), NSForegroundColorAttributeName, DTColorCreateWithHTMLName(@"blue"), NSBackgroundColorAttributeName, nil];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"string" attributes:buildAttributes];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTColor *color = [attributes foregroundColor];
NSString *hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"ff0000"], @"Default Color should be red");
color = [attributes backgroundColor];
hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"0000ff"], @"Default Color should be blue");
}
// this crashes or hangs issue #648
- (void)testNSValidColorsFromHTML
{
if (!DTCoreTextModernAttributesPossible())
{
return;
}
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:DTUseiOS6Attributes];
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<span style=\"color:red;background-color:blue;\">Paragraph</span>" options:options];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
DTColor *color = [attributes foregroundColor];
NSString *hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"ff0000"], @"Default Color should be red");
color = [attributes backgroundColor];
hexColor = DTHexStringFromDTColor(color);
XCTAssertTrue([hexColor isEqualToString:@"0000ff"], @"Default Color should be blue");
}
- (void)testKerning
{
NSAttributedString *attributedString = [self attributedStringFromHTMLString:@"<p style=\"letter-spacing:10px\">Paragraph</p>" options:NULL];
NSDictionary *attributes = [attributedString attributesAtIndex:0 effectiveRange:NULL];
CGFloat kerning = [attributes kerning];
XCTAssertEqual(kerning, (CGFloat)10.0, @"Kerning incorrect");
}
@end