我可以在iOS的Core Text中使用NSAttributedString吗?

jow*_*wie 10 iphone objective-c ipad core-text

我正在尝试研究如何获取NSAttributedString并在iPad上的Core Text中使用它.我观看了一个WWDC视频(110),其中有幻灯片(但没有源代码),它描述了如何创建NSAttributedString,然后将其放入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);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这个时,它会抛出错误:

从不兼容的指针类型传递'CTFramesetterCreateWithAttributedString'的参数1

CFAttributedString和NSAttributedString'免费桥接'?我已经在某处读过,这只适用于OSX,但这就是Apple在他们的演示中如何做到这一点......

谢谢!

:-Joe

jer*_*jer 8

您可以在此处下载WWDC10源代码.此外,使用免费桥接并明确地投射stringToDrawCFAttributedStringRef.