【发布时间】:2011-04-15 16:34:53
【问题描述】:
我正在尝试研究如何获取 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);
但是当我尝试这个时,它会抛出错误:
从不兼容的指针类型传递“CTFramesetterCreateWithAttributedString”的参数 1
CFAttributedString 和 NSAttributedString 是否“免费桥接”?我在某处读到这仅在 OSX 上是正确的,但这就是 Apple 在他们的演示中这样做的方式......
谢谢!
:-乔
【问题讨论】:
-
您是否能够显示虚线下划线?我只在文本下得到一个简单的单下划线。
标签: iphone objective-c ipad core-text