Skip to content

Commit

Permalink
fix cache key issue by removing the memory address from CTParagraphSt…
Browse files Browse the repository at this point in the history
…yleRef description
  • Loading branch information
tonycn committed Apr 10, 2012
1 parent 07a4dd5 commit 33319d1
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions Core/Source/DTCoreTextParagraphStyle.m
Expand Up @@ -42,6 +42,23 @@ + (DTCoreTextParagraphStyle *)defaultParagraphStyle
return [[DTCoreTextParagraphStyle alloc] init];
}

+ (NSString *)niceKeyFromParagraghStyle:(CTParagraphStyleRef)ctParagraphStyle {

// this is naughty: CTParagraphStyle has a description
NSString *key = [(__bridge id)ctParagraphStyle description];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"0x[0123456789abcdef]{1,8}"
options:NSRegularExpressionCaseInsensitive
error:nil];

NSString *newKey = [regex stringByReplacingMatchesInString:key
options:0
range:NSMakeRange(0, [key length])
withTemplate:@""];

return newKey;
}

+ (DTCoreTextParagraphStyle *)paragraphStyleWithCTParagraphStyle:(CTParagraphStyleRef)ctParagraphStyle
{
DTCoreTextParagraphStyle *returnParagraphStyle = NULL;
Expand All @@ -58,13 +75,7 @@ + (DTCoreTextParagraphStyle *)paragraphStyleWithCTParagraphStyle:(CTParagraphSty
dispatch_semaphore_wait(selfLock, DISPATCH_TIME_FOREVER);
{

// this is naughty: CTParagraphStyle has a description
NSString *key = [(__bridge id)ctParagraphStyle description];
// a trick to remove "<CFArray 0x7ea2e50 [0x20eab48]>" from key
NSRange firstLessThan = [key rangeOfString:@"<"];
if (firstLessThan.location != NSNotFound) {
key = [key substringToIndex:firstLessThan.location];
}
NSString *key = [self niceKeyFromParagraghStyle:ctParagraphStyle];
returnParagraphStyle = [_paragraphStyleCache objectForKey:key];

if (!returnParagraphStyle)
Expand Down

0 comments on commit 33319d1

Please sign in to comment.