Can I use NSAttributedString in Core Text in iOS?
Asked Answered
E

3

10

I'm trying to work out how to take an NSAttributedString and use it in Core Text on the iPad. I watched one of the WWDC videos (110) which has slides (but no source code) and it describes how to create an NSAttributedString, then just put it into a CTFramesetterRef:

CTFontRef helveticaBold = CTFontCreateWithName( CFSTR("Helvetica-Bold"), 24.0, NULL);

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title");
CGColorRef color = [UIColor blueColor].CGColor; NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle|kCTUnderlinePatternDot];
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:helveticaBold, (NSString*)kCTFontAttributeName, color, (NSString*)kCTForegroundColorAttributeName, underline, (NSString*)kCTUnderlineStyleAttributeName, nil];
NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict];

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(stringToDraw);

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, context);
CFRelease(frame);

But when I try this, it throws the error:

Passing argument 1 of 'CTFramesetterCreateWithAttributedString' from incompatible pointer type

Are CFAttributedString and NSAttributedString 'toll-free bridged'? I've read somewhere that this is only true on OSX, but this is how Apple do it in their demo...

Thanks!

:-Joe

Embrey answered 22/9, 2010 at 11:38 Comment(1)
were you able to get the dotted underline to show up? I only get the plain single underline under the text.Dimer
R
8

You can download the WWDC10 source code here. In addition, use toll-free bridging and explicitly cast your stringToDraw to a CFAttributedStringRef.

Resnick answered 22/9, 2010 at 11:52 Comment(2)
Thanks, but the link you gave keeps saying my session has expired (even when I try to login again). I have downloaded the WWDC10 sample code before, but 110 wasn't in it :(Embrey
Just change NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict]; to CFAttributedStringRef stringToDraw = (CFAttributedStringRef)[[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict];Embrey
J
4

You're almost there. Just cast the CTFontRef to an id type when you add it to the attributes dictionary.

CTFontRef helveticaBold = CTFontCreateWithName( CFSTR("Helvetica-Bold"), 24.0, NULL);

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title");

CGColorRef color = [UIColor blueColor].CGColor; 
NSNumber* underline = [NSNumber numberWithInt:
    kCTUnderlineStyleSingle|kCTUnderlinePatternDot];

NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
                    (id)helveticaBold, (NSString*)kCTFontAttributeName, 
                    color, (NSString*)kCTForegroundColorAttributeName, 
                    underline, (NSString*)kCTUnderlineStyleAttributeName, nil];

NSAttributedString* stringToDraw = [[NSAttributedString alloc] 
                            initWithString:unicodeString
                            attributes:attributesDict];
Jovanjove answered 24/4, 2012 at 13:26 Comment(0)
C
0

the code of WWDC Session 110 CTPageViewer is now part of Apple's official sample code:

http://developer.apple.com/library/ios/#samplecode/CoreTextPageViewer/Listings/ReadMe_txt.html

Cranial answered 19/9, 2012 at 17:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.