forked from mccraigmccraig/colloquy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSTextStorageAdditions.m
97 lines (79 loc) · 4.28 KB
/
NSTextStorageAdditions.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
#import "NSTextStorageAdditions.h"
@implementation NSTextStorage (NSTextStorageAdditions)
- (NSColor *) backgroundColor {
id color = [self attribute:NSBackgroundColorAttributeName atIndex:0 effectiveRange:NULL];
if( [color isKindOfClass:[NSColor class]] ) return color;
return (id)[NSNull null];
}
- (void) setBackgroundColor:(NSColor *) color {
if( ! color || ! [color isKindOfClass:[NSColor class]] ) [self removeAttribute:NSBackgroundColorAttributeName range:NSMakeRange( 0, [self length] )];
else [self addAttribute:NSBackgroundColorAttributeName value:color range:NSMakeRange( 0, [self length] )];
}
- (NSString *) hyperlink {
id link = [self attribute:NSLinkAttributeName atIndex:0 effectiveRange:NULL];
if( [link isKindOfClass:[NSURL class]] ) return [link absoluteString];
else if( [link isKindOfClass:[NSString class]] ) return link;
return (id)[NSNull null];
}
- (void) setHyperlink:(NSString *) link {
if( ! [link isKindOfClass:[NSString class]] || ! [link length] ) [self removeAttribute:NSLinkAttributeName range:NSMakeRange( 0, [self length] )];
else [self addAttribute:NSLinkAttributeName value:link range:NSMakeRange( 0, [self length] )];
}
- (BOOL) boldState {
NSFont *font = [self attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
int traits = [[NSFontManager sharedFontManager] traitsOfFont:font];
return ( traits & NSBoldFontMask );
}
- (void) setBoldState:(BOOL) bold {
NSFont *font = [self attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
if( bold ) font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSBoldFontMask];
else font = [[NSFontManager sharedFontManager] convertFont:font toNotHaveTrait:NSBoldFontMask];
if( font ) [self addAttribute:NSFontAttributeName value:font range:NSMakeRange( 0, [self length] )];
}
- (BOOL) italicState {
NSFont *font = [self attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
int traits = [[NSFontManager sharedFontManager] traitsOfFont:font];
return ( traits & NSItalicFontMask );
}
- (void) setItalicState:(BOOL) italic {
NSFont *font = [self attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
if( bold ) font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSItalicFontMask];
else font = [[NSFontManager sharedFontManager] convertFont:font toNotHaveTrait:NSItalicFontMask];
if( font ) [self addAttribute:NSFontAttributeName value:font range:NSMakeRange( 0, [self length] )];
}
- (BOOL) underlineState {
return [[self attribute:NSUnderlineStyleAttributeName atIndex:0 effectiveRange:NULL] boolValue];
}
- (void) setUnderlineState:(BOOL) underline {
if( ! underline ) [self removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange( 0, [self length] )];
else [self addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithBool:1] range:NSMakeRange( 0, [self length] )];
}
- (NSArray *) styleClasses {
return [[self attribute:@"CSSClasses" atIndex:0 effectiveRange:NULL] allObjects];
}
- (void) setStyleClasses:(NSArray *) classes {
if( ! [classes isKindOfClass:[NSArray class]] || ! [classes count] ) [self removeAttribute:@"CSSClasses" range:NSMakeRange( 0, [self length] )];
else [self addAttribute:@"CSSClasses" value:[NSSet setWithArray:classes] range:NSMakeRange( 0, [self length] )];
}
- (NSString *) styleText {
return [self attribute:@"CSSText" atIndex:0 effectiveRange:NULL];
}
- (void) setStyleText:(NSString *) style {
if( ! [style isKindOfClass:[NSString class]] || ! [style length] ) [self removeAttribute:@"CSSText" range:NSMakeRange( 0, [self length] )];
else [self addAttribute:@"CSSText" value:style range:NSMakeRange( 0, [self length] )];
}
- (NSString *) XHTMLStart {
return [self attribute:@"XHTMLStart" atIndex:0 effectiveRange:NULL];
}
- (void) setXHTMLStart:(NSString *) html {
if( ! [html isKindOfClass:[NSString class]] ) [self removeAttribute:@"XHTMLStart" range:NSMakeRange( 0, [self length] )];
else [self addAttribute:@"XHTMLStart" value:html range:NSMakeRange( 0, [self length] )];
}
- (NSString *) XHTMLEnd {
return [self attribute:@"XHTMLEnd" atIndex:0 effectiveRange:NULL];
}
- (void) setXHTMLEnd:(NSString *) html {
if( ! [html isKindOfClass:[NSString class]] ) [self removeAttribute:@"XHTMLEnd" range:NSMakeRange( 0, [self length] )];
else [self addAttribute:@"XHTMLEnd" value:html range:NSMakeRange( 0, [self length] )];
}
@end