首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以在NSAttributedString中使用iOS的核心文本吗?

我可以在NSAttributedString中使用iOS的核心文本吗?
EN

Stack Overflow用户
提问于 2010-09-22 11:38:02
回答 3查看 14.9K关注 0票数 10

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

代码语言:javascript
运行
复制
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上是正确的,但这是苹果在他们的演示中所做的.

谢谢!

-乔

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-22 11:52:58

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

票数 8
EN

Stack Overflow用户

发布于 2012-04-24 13:26:23

你快到了。将CTFontRef添加到属性字典时,只需将其转换为id类型即可。

代码语言:javascript
运行
复制
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];
票数 4
EN

Stack Overflow用户

发布于 2012-09-19 17:04:45

WWDC Session 110 CTPageViewer的代码现在是苹果官方示例代码的一部分:

txt.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3768801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档