| 1 |
#import "TextFormatter.h" |
| 2 |
|
| 3 |
|
| 4 |
@implementation TextFormatter |
| 5 |
-(NSDictionary *)attributedDictionary{ |
| 6 |
|
| 7 |
|
| 8 |
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 9 |
_foregroundColor, NSForegroundColorAttributeName, |
| 10 |
_backgroundColor, NSBackgroundColorAttributeName, |
| 11 |
_font, NSFontAttributeName, |
| 12 |
nil]; |
| 13 |
return dict; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
-(id)initWithName:(NSString *)name |
| 18 |
pattern:(NSString *)pattern |
| 19 |
foregroundColor:(NSColor *)foregroundColor |
| 20 |
backgroundColor:(NSColor *)backgroundColor |
| 21 |
font:(NSFont *)font{ |
| 22 |
self = [super init]; |
| 23 |
_name = [name retain]; |
| 24 |
_pattern = [pattern retain]; |
| 25 |
_foregroundColor = [foregroundColor retain]; |
| 26 |
_backgroundColor = [backgroundColor retain]; |
| 27 |
_font = [font retain]; |
| 28 |
return self; |
| 29 |
} |
| 30 |
-(void)dealloc{ |
| 31 |
[_name release]; |
| 32 |
[_pattern release]; |
| 33 |
[_foregroundColor release]; |
| 34 |
[_backgroundColor release]; |
| 35 |
[_font release]; |
| 36 |
[super dealloc]; |
| 37 |
} |
| 38 |
-(NSString *)name{ |
| 39 |
return _name; |
| 40 |
} |
| 41 |
-(NSString *)pattern{ |
| 42 |
return _pattern; |
| 43 |
} |
| 44 |
-(NSColor *)foregroundColor{ |
| 45 |
return _foregroundColor; |
| 46 |
} |
| 47 |
-(NSColor *)backgroundColor{ |
| 48 |
return _backgroundColor; |
| 49 |
} |
| 50 |
-(NSFont *)font{ |
| 51 |
return _font; |
| 52 |
} |
| 53 |
-(NSAttributedString *)formatWithAttributedString: |
| 54 |
(NSAttributedString *)attributedString{ |
| 55 |
NSString *string; |
| 56 |
NSMutableAttributedString *ret; |
| 57 |
NSRange patternRange; |
| 58 |
NSRange stringRange; |
| 59 |
|
| 60 |
|
| 61 |
ret = [[[NSMutableAttributedString alloc] init] autorelease]; |
| 62 |
[ret appendAttributedString:attributedString]; |
| 63 |
|
| 64 |
// pattern������������������������������������������������������ |
| 65 |
string = [attributedString string]; |
| 66 |
if([_pattern isEqual:@""]){ |
| 67 |
[ret setAttributes:[self attributedDictionary] |
| 68 |
range:NSMakeRange(0, [string length])]; |
| 69 |
return ret; |
| 70 |
} |
| 71 |
|
| 72 |
stringRange = NSMakeRange(0, [string length]); |
| 73 |
while(stringRange.length > 0){ |
| 74 |
patternRange = [string rangeOfString:_pattern |
| 75 |
options:0 |
| 76 |
range:stringRange]; |
| 77 |
if(patternRange.location == NSNotFound){ |
| 78 |
return ret; |
| 79 |
} |
| 80 |
[ret setAttributes:[self attributedDictionary] range:patternRange]; |
| 81 |
stringRange.location = NSMaxRange(patternRange); |
| 82 |
stringRange.length -= NSMaxRange(patternRange); |
| 83 |
} |
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
@end |