我可以在iOS的Core Text中使用NSAttributedString吗? [英] Can I use NSAttributedString in Core Text in iOS?

查看:152
本文介绍了我可以在iOS的Core Text中使用NSAttributedString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究如何获取NSAttributedString并在iPad上的Core Text中使用它。我观看了其中一个有幻灯片(但没有源代码)的WWDC视频(110),它描述了如何创建NSAttributedString,然后将其放入CTFramesetterRef:

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:


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

Passing argument 1 of 'CTFramesetterCreateWithAttributedString' from incompatible pointer type

是CFAttributedString和NSAttributedString '免费桥接'?我已经在某个地方读过,这只适用于OSX,但这就是Apple在他们的演示中的表现......

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...

谢谢!

: - Joe

推荐答案

您可以下载WWDC10源代码此处。此外,使用免费桥接并将 stringToDraw 显式转换为 CFAttributedStringRef

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

这篇关于我可以在iOS的Core Text中使用NSAttributedString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆