Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (53 sloc) 2.088 kb
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
//
// NSMutableAttributedString+HTML.m
// CoreTextExtensions
//
// Created by Oliver Drobnik on 4/14/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//

#import "NSMutableAttributedString+HTML.h"

#import "DTCoreText.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];
}

// need to remove image placeholder to prevent duplication
if ([previousAttributes objectForKey:NSAttachmentAttributeName])
{
NSMutableDictionary *tmpDict = [previousAttributes mutableCopy];

[tmpDict removeObjectForKey:NSAttachmentAttributeName];
[tmpDict removeObjectForKey:(id)kCTRunDelegateAttributeName];

previousAttributes = tmpDict;
}

NSAttributedString *tmpString = [[NSAttributedString alloc] initWithString:string attributes:previousAttributes];
[self appendAttributedString:tmpString];
}

- (void)appendString:(NSString *)string withParagraphStyle:(DTCoreTextParagraphStyle *)paragraphStyle fontDescriptor:(DTCoreTextFontDescriptor *)fontDescriptor
{
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];

if (paragraphStyle)
{
CTParagraphStyleRef newParagraphStyle = [paragraphStyle createCTParagraphStyle];
[attributes setObject:CFBridgingRelease(newParagraphStyle) forKey:(id)kCTParagraphStyleAttributeName];
}

if (fontDescriptor)
{
CTFontRef newFont = [fontDescriptor newMatchingFont];
[attributes setObject:CFBridgingRelease(newFont) forKey:(id)kCTFontAttributeName];
}

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
[self appendAttributedString:attributedString];
}

// appends a string without any attributes
- (void)appendNakedString:(NSString *)string
{
[self appendString:string withParagraphStyle:nil fontDescriptor:nil];
}

@end
Something went wrong with that request. Please try again.