Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved list prefix generation to category

  • Loading branch information...
commit 533d5ea69449968f44c3c805c8bc0c221531b523 1 parent 6006891
@Cocoanetics Cocoanetics authored
View
3  Core/Source/DTHTMLAttributedStringBuilder.m
@@ -820,7 +820,8 @@ - (void)_flushListPrefix
counter = [effectiveList startingItemNumber];
}
- NSAttributedString *prefixString = [currentTag prefixForListItemWithCounter:counter];
+ NSDictionary *tagAttributes = [currentTag attributesDictionary];
+ NSAttributedString *prefixString = [NSAttributedString prefixForListItemWithCounter:counter listStyle:effectiveList attributes:tagAttributes];
if (prefixString)
{
View
1  Core/Source/DTHTMLElement.h
@@ -61,7 +61,6 @@ typedef enum
@property (nonatomic, strong) NSDictionary *attributes;
- (NSAttributedString *)attributedString;
-- (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter;
- (NSDictionary *)attributesDictionary;
- (void)parseStyleString:(NSString *)styleString;
View
96 Core/Source/DTHTMLElement.m
@@ -250,102 +250,6 @@ - (NSAttributedString *)attributedString
}
}
-- (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter
-{
- NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
-
- if (fontDescriptor)
- {
- // make a font without italic or bold
- DTCoreTextFontDescriptor *fontDesc = [self.fontDescriptor copy];
-
- fontDesc.boldTrait = NO;
- fontDesc.italicTrait = NO;
-
- CTFontRef font = [fontDesc newMatchingFont];
-
- [attributes setObject:CFBridgingRelease(font) forKey:(id)kCTFontAttributeName];
- }
-
- // text color for bullet same as text
- if (_textColor)
- {
- [attributes setObject:(id)[_textColor CGColor] forKey:(id)kCTForegroundColorAttributeName];
- }
- // add paragraph style (this has the tabs)
- if (paragraphStyle)
- {
- CTParagraphStyleRef newParagraphStyle = [self.paragraphStyle createCTParagraphStyle];
- [attributes setObject:CFBridgingRelease(newParagraphStyle) forKey:(id)kCTParagraphStyleAttributeName];
- }
-
- // add a marker so that we know that this is a field/prefix
- [attributes setObject:@"{listprefix}" forKey:DTFieldAttribute];
-
- // get calculated list style
- DTCSSListStyle *calculatedListStyle = [self.paragraphStyle.textLists lastObject];// [self calculatedListStyle];
-
- if (calculatedListStyle)
- {
- [attributes setObject:self.paragraphStyle.textLists forKey:DTTextListsAttribute];
- }
-
- NSString *prefix = [calculatedListStyle prefixWithCounter:listCounter];
-
- if (prefix)
- {
- DTImage *image = nil;
-
- if (calculatedListStyle.imageName)
- {
- image = [DTImage imageNamed:calculatedListStyle.imageName];
-
- if (!image)
- {
- // image invalid
- calculatedListStyle.imageName = nil;
-
- prefix = [calculatedListStyle prefixWithCounter:listCounter];
- }
- }
-
- NSMutableAttributedString *tmpStr = [[NSMutableAttributedString alloc] initWithString:prefix attributes:attributes];
-
-
- if (image)
- {
- // make an attachment for the image
- DTTextAttachment *attachment = [[DTTextAttachment alloc] init];
- attachment.contents = image;
- attachment.contentType = DTTextAttachmentTypeImage;
- attachment.displaySize = image.size;
-
-#if TARGET_OS_IPHONE
- // need run delegate for sizing
- CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(attachment);
- [attributes setObject:CFBridgingRelease(embeddedObjectRunDelegate) forKey:(id)kCTRunDelegateAttributeName];
-#endif
-
- // add attachment
- [attributes setObject:attachment forKey:NSAttachmentAttributeName];
-
- if (calculatedListStyle.position == DTCSSListStylePositionInside)
- {
- [tmpStr setAttributes:attributes range:NSMakeRange(2, 1)];
- }
- else
- {
- [tmpStr setAttributes:attributes range:NSMakeRange(1, 1)];
- }
- }
-
- return tmpStr;
- }
-
- return nil;
-}
-
-
- (void)applyStyleDictionary:(NSDictionary *)styles
{
if (![styles count])
View
18 Core/Source/NSAttributedString+DTCoreText.h
@@ -86,4 +86,22 @@
*/
- (NSString *)plainTextString;
+
+/**
+ @name Creating Special Attributed Strings
+ */
+
+
+/**
+ Create a prefix for a paragraph in a list
+
+ @param listCounter The value for the list item.
+ @param listStyle The list style
+ @param attributes The attribute dictionary for the text to be prefixed
+ @returns An attributed string with the list prefix
+ */
++ (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter listStyle:(DTCSSListStyle *)listStyle attributes:(NSDictionary *)attributes;
+
+
+
@end
View
131 Core/Source/NSAttributedString+DTCoreText.m
@@ -9,6 +9,13 @@
#import "DTCoreText.h"
#import "NSAttributedString+DTCoreText.h"
+// use smaller list indent on iPhone OS
+#if TARGET_OS_IPHONE
+#define SPECIAL_LIST_INDENT 27.0f
+#else
+#define SPECIAL_LIST_INDENT 36.0f
+#endif
+
@implementation NSAttributedString (DTCoreText)
#pragma mark Text Attachments
@@ -729,4 +736,128 @@ - (NSString *)plainTextString
return [tmpString stringByReplacingOccurrencesOfString:UNICODE_OBJECT_PLACEHOLDER withString:@""];
}
+#pragma Generating Special Attributed Strings
++ (NSAttributedString *)prefixForListItemWithCounter:(NSUInteger)listCounter listStyle:(DTCSSListStyle *)listStyle attributes:(NSDictionary *)attributes
+{
+ // get existing values from attributes
+ CTParagraphStyleRef paraStyle = (__bridge CTParagraphStyleRef)[attributes objectForKey:(id)kCTParagraphStyleAttributeName];
+ CTFontRef font = (__bridge CTFontRef)[attributes objectForKey:(id)kCTFontAttributeName];
+ CGColorRef textColor = (__bridge CGColorRef)[attributes objectForKey:(id)kCTForegroundColorAttributeName];
+
+ DTCoreTextFontDescriptor *fontDescriptor = nil;
+ DTCoreTextParagraphStyle *paragraphStyle = nil;
+
+ if (paraStyle)
+ {
+ paragraphStyle = [DTCoreTextParagraphStyle paragraphStyleWithCTParagraphStyle:paraStyle];
+
+ paragraphStyle.tabStops = nil;
+
+ paragraphStyle.headIndent = SPECIAL_LIST_INDENT;
+ paragraphStyle.paragraphSpacing = 0;
+
+ // first tab is to right-align bullet, numbering against
+ CGFloat tabOffset = paragraphStyle.headIndent - 5.0f*1.0; // TODO: change with font size
+ [paragraphStyle addTabStopAtPosition:tabOffset alignment:kCTRightTextAlignment];
+
+ // second tab is for the beginning of first line after bullet
+ [paragraphStyle addTabStopAtPosition:paragraphStyle.headIndent alignment: kCTLeftTextAlignment];
+ }
+
+ if (font)
+ {
+ fontDescriptor = [DTCoreTextFontDescriptor fontDescriptorForCTFont:font];
+ }
+
+ NSMutableDictionary *newAttributes = [NSMutableDictionary dictionary];
+
+ if (fontDescriptor)
+ {
+ // make a font without italic or bold
+ DTCoreTextFontDescriptor *fontDesc = [fontDescriptor copy];
+
+ fontDesc.boldTrait = NO;
+ fontDesc.italicTrait = NO;
+
+ CTFontRef font = [fontDesc newMatchingFont];
+
+ [newAttributes setObject:CFBridgingRelease(font) forKey:(id)kCTFontAttributeName];
+ }
+
+ // text color for bullet same as text
+ if (textColor)
+ {
+ [newAttributes setObject:(__bridge id)textColor forKey:(id)kCTForegroundColorAttributeName];
+ }
+
+ // add paragraph style (this has the tabs)
+ if (paragraphStyle)
+ {
+ CTParagraphStyleRef newParagraphStyle = [paragraphStyle createCTParagraphStyle];
+ [newAttributes setObject:CFBridgingRelease(newParagraphStyle) forKey:(id)kCTParagraphStyleAttributeName];
+ }
+
+ if (listStyle)
+ {
+ [newAttributes setObject:[NSArray arrayWithObject:listStyle] forKey:DTTextListsAttribute];
+ }
+
+ // add a marker so that we know that this is a field/prefix
+ [newAttributes setObject:@"{listprefix}" forKey:DTFieldAttribute];
+
+ NSString *prefix = [listStyle prefixWithCounter:listCounter];
+
+ if (prefix)
+ {
+ DTImage *image = nil;
+
+ if (listStyle.imageName)
+ {
+ image = [DTImage imageNamed:listStyle.imageName];
+
+ if (!image)
+ {
+ // image invalid
+ listStyle.imageName = nil;
+
+ prefix = [listStyle prefixWithCounter:listCounter];
+ }
+ }
+
+ NSMutableAttributedString *tmpStr = [[NSMutableAttributedString alloc] initWithString:prefix attributes:newAttributes];
+
+
+ if (image)
+ {
+ // make an attachment for the image
+ DTTextAttachment *attachment = [[DTTextAttachment alloc] init];
+ attachment.contents = image;
+ attachment.contentType = DTTextAttachmentTypeImage;
+ attachment.displaySize = image.size;
+
+#if TARGET_OS_IPHONE
+ // need run delegate for sizing
+ CTRunDelegateRef embeddedObjectRunDelegate = createEmbeddedObjectRunDelegate(attachment);
+ [newAttributes setObject:CFBridgingRelease(embeddedObjectRunDelegate) forKey:(id)kCTRunDelegateAttributeName];
+#endif
+
+ // add attachment
+ [newAttributes setObject:attachment forKey:NSAttachmentAttributeName];
+
+ if (listStyle.position == DTCSSListStylePositionInside)
+ {
+ [tmpStr setAttributes:newAttributes range:NSMakeRange(2, 1)];
+ }
+ else
+ {
+ [tmpStr setAttributes:newAttributes range:NSMakeRange(1, 1)];
+ }
+ }
+
+ return tmpStr;
+ }
+
+ return nil;
+}
+
@end
View
12 DTCoreText.xcodeproj/project.pbxproj
@@ -186,7 +186,7 @@
A788CA4D14863EF100E1AFD9 /* WarAndPeace.html in Resources */ = {isa = PBXBuildFile; fileRef = A788CA2E14863EF100E1AFD9 /* WarAndPeace.html */; };
A788CA4E14863EF100E1AFD9 /* XB Niloofar.ttf in Resources */ = {isa = PBXBuildFile; fileRef = A788CA2F14863EF100E1AFD9 /* XB Niloofar.ttf */; };
A788CA4F14863EF100E1AFD9 /* XB NiloofarBd.ttf in Resources */ = {isa = PBXBuildFile; fileRef = A788CA3014863EF100E1AFD9 /* XB NiloofarBd.ttf */; };
- A788CA5014863EF100E1AFD9 /* DemoAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A788CA3414863EF100E1AFD9 /* DemoAppDelegate.m */; };
+ A788CA5014863EF100E1AFD9 /* CoreTextDemoAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A788CA3414863EF100E1AFD9 /* CoreTextDemoAppDelegate.m */; };
A788CA5114863EF100E1AFD9 /* DemoSnippetsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A788CA3614863EF100E1AFD9 /* DemoSnippetsViewController.m */; };
A788CA5214863EF100E1AFD9 /* DemoTextViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A788CA3814863EF100E1AFD9 /* DemoTextViewController.m */; };
A788CA5314863EF100E1AFD9 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A788CA3914863EF100E1AFD9 /* main.m */; };
@@ -484,8 +484,8 @@
A788CA2E14863EF100E1AFD9 /* WarAndPeace.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = WarAndPeace.html; sourceTree = "<group>"; };
A788CA2F14863EF100E1AFD9 /* XB Niloofar.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "XB Niloofar.ttf"; sourceTree = "<group>"; };
A788CA3014863EF100E1AFD9 /* XB NiloofarBd.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "XB NiloofarBd.ttf"; sourceTree = "<group>"; };
- A788CA3314863EF100E1AFD9 /* DemoAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoAppDelegate.h; sourceTree = "<group>"; };
- A788CA3414863EF100E1AFD9 /* DemoAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoAppDelegate.m; sourceTree = "<group>"; };
+ A788CA3314863EF100E1AFD9 /* CoreTextDemoAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoreTextDemoAppDelegate.h; sourceTree = "<group>"; };
+ A788CA3414863EF100E1AFD9 /* CoreTextDemoAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CoreTextDemoAppDelegate.m; sourceTree = "<group>"; };
A788CA3514863EF100E1AFD9 /* DemoSnippetsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoSnippetsViewController.h; sourceTree = "<group>"; };
A788CA3614863EF100E1AFD9 /* DemoSnippetsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = DemoSnippetsViewController.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
A788CA3714863EF100E1AFD9 /* DemoTextViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoTextViewController.h; sourceTree = "<group>"; };
@@ -782,8 +782,8 @@
A788CA3114863EF100E1AFD9 /* Source */ = {
isa = PBXGroup;
children = (
- A788CA3314863EF100E1AFD9 /* DemoAppDelegate.h */,
- A788CA3414863EF100E1AFD9 /* DemoAppDelegate.m */,
+ A788CA3314863EF100E1AFD9 /* CoreTextDemoAppDelegate.h */,
+ A788CA3414863EF100E1AFD9 /* CoreTextDemoAppDelegate.m */,
A788CA3514863EF100E1AFD9 /* DemoSnippetsViewController.h */,
A788CA3614863EF100E1AFD9 /* DemoSnippetsViewController.m */,
A788CA3714863EF100E1AFD9 /* DemoTextViewController.h */,
@@ -1297,7 +1297,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
- A788CA5014863EF100E1AFD9 /* DemoAppDelegate.m in Sources */,
+ A788CA5014863EF100E1AFD9 /* CoreTextDemoAppDelegate.m in Sources */,
A788CA5114863EF100E1AFD9 /* DemoSnippetsViewController.m in Sources */,
A788CA5214863EF100E1AFD9 /* DemoTextViewController.m in Sources */,
A788CA5314863EF100E1AFD9 /* main.m in Sources */,
View
2  Demo/Source/DemoAppDelegate.h → Demo/Source/CoreTextDemoAppDelegate.h
@@ -8,7 +8,7 @@
@class DemoTextViewController;
-@interface DemoAppDelegate : NSObject <UIApplicationDelegate>
+@interface CoreTextDemoAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *_window;
UINavigationController *_navigationController;
View
4 Demo/Source/DemoAppDelegate.m → Demo/Source/CoreTextDemoAppDelegate.m
@@ -6,10 +6,10 @@
// Copyright 2011 Drobnik.com. All rights reserved.
//
-#import "DemoAppDelegate.h"
+#import "CoreTextDemoAppDelegate.h"
#import "DemoSnippetsViewController.h"
-@implementation DemoAppDelegate
+@implementation CoreTextDemoAppDelegate
@synthesize window = _window;
View
2  Demo/Source/main.m
@@ -9,7 +9,7 @@
int main(int argc, char *argv[]) {
@autoreleasepool {
- int retVal = UIApplicationMain(argc, argv, nil, @"DemoAppDelegate");
+ int retVal = UIApplicationMain(argc, argv, nil, @"CoreTextDemoAppDelegate");
return retVal;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.