首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CTFont对象作为NSFont对象,桥不工作

使用CTFont对象作为NSFont对象,桥不工作
EN

Stack Overflow用户
提问于 2016-09-28 06:52:55
回答 1查看 565关注 0票数 0

我正在尝试将使用NSFont的代码转换为使用CTFont的代码(试图使其成为macOS和iOS的跨平台)。

根据文档和this帖子,我不需要做任何事情就可以让它工作,因为在CTFontRef和NSFont之间有一座免费的桥。

后来,我得到了这个错误

代码语言:javascript
复制
error: cannot initialize a parameter of type 'NSFont * _Nonnull' with an lvalue of type 'CTFontRef' (aka 'const __CTFont *')
        convertedFont = [fontManager convertFont:convertedFont toNotHaveTrait:NSBoldFontMask];
                                                  ^~~~~~~~~~~~~

旧代码:(暂时保留NSFontManager )

代码语言:javascript
复制
NSFontManager *fontManager = [NSFontManager sharedFontManager];
//            NSFont *font = static_cast<NSFont *>(inProperties[theKey]);
//            NSFontTraitMask fontTraits = [fontManager traitsOfFont:font];
//            NSFont *convertedFont = font;

新代码:

代码语言:javascript
复制
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)inProperties);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
CFRelease(descriptor);
CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
CTFontRef convertedFont = font;
EN

回答 1

Stack Overflow用户

发布于 2017-02-16 18:55:23

NSFontCTFont之间的免费桥梁意味着:

给定一个NSFont对象,它可以直接转换为CTFont对象。

代码语言:javascript
复制
NSFont *fontObj = [NSFont systemFontOfSize:12];
CTFontRef coreFont = (__bridge CTFontRef)(fontObj);

在给定CTFont对象的情况下,可以将其直接转换为NSFont对象。

代码语言:javascript
复制
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)inProperties);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
CFRelease(descriptor);
NSFont *nsFont = (__bridge NSFont *)(font);

我认为您在代码中犯的错误是没有将NSFont对象转换为CTFont对象。

如果它解决了问题,请告诉我

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

https://stackoverflow.com/questions/39735557

复制
相关文章

相似问题

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