Permalink
Browse files

fix cache key issue by removing the memory address from CTParagraphSt…

…yleRef description
  • Loading branch information...
1 parent 07a4dd5 commit 33319d14988aed52128c7cec8ee901d5cf65334b @tonycn tonycn committed Apr 10, 2012
Showing with 18 additions and 7 deletions.
  1. +18 −7 Core/Source/DTCoreTextParagraphStyle.m
@@ -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;
@@ -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)

0 comments on commit 33319d1

Please sign in to comment.