我正在努力研究如何获取NSAttributedString并在iPad上的核心文本中使用它。我观看了一个包含幻灯片(但没有源代码)的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上是正确的,但这是苹果在他们的演示中所做的.
谢谢!
-乔
发布于 2010-09-22 11:52:58
您可以下载WWDC10源代码这里。此外,使用免费桥接并显式地将stringToDraw转换为CFAttributedStringRef。
发布于 2012-04-24 13:26:23
你快到了。将CTFontRef添加到属性字典时,只需将其转换为id类型即可。
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];发布于 2012-09-19 17:04:45
WWDC Session 110 CTPageViewer的代码现在是苹果官方示例代码的一部分:
txt.html
https://stackoverflow.com/questions/3768801
复制相似问题