forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSMutableAttributedString+HTML.m
61 lines (46 loc) · 1.62 KB
/
NSMutableAttributedString+HTML.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
//
// NSMutableAttributedString+HTML.m
// CoreTextExtensions
//
// Created by Oliver Drobnik on 4/14/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#import "NSMutableAttributedString+HTML.h"
#import <CoreText/CoreText.h>
#import "DTCoreTextParagraphStyle.h"
//#import "DTRangedAttributesOptimizer.h"
@implementation NSMutableAttributedString (HTML)
// apends a plain string extending the attributes at this position
- (void)appendString:(NSString *)string
{
NSInteger length = [self length];
NSDictionary *previousAttributes = nil;
if (length)
{
// get attributes from last character
previousAttributes = [self attributesAtIndex:length-1 effectiveRange:NULL];
}
NSAttributedString *tmpString = [[NSAttributedString alloc] initWithString:string attributes:previousAttributes];
[self appendAttributedString:tmpString];
[tmpString release];
}
- (void)appendString:(NSString *)string withParagraphStyle:(DTCoreTextParagraphStyle *)paragraphStyle
{
NSMutableDictionary *attributes = nil;
if (paragraphStyle)
{
attributes = [NSMutableDictionary dictionary];
CTParagraphStyleRef newParagraphStyle = [paragraphStyle createCTParagraphStyle];
[attributes setObject:(id)newParagraphStyle forKey:(id)kCTParagraphStyleAttributeName];
CFRelease(newParagraphStyle);
}
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
[self appendAttributedString:attributedString];
[attributedString release];
}
// appends a string without any attributes
- (void)appendNakedString:(NSString *)string
{
[self appendString:string withParagraphStyle:nil];
}
@end