简体   繁体   English

子类化NSFont

[英]Subclasssing NSFont

Trying to write some common libraries that work between iOS and OS X. In order to not clutter up the apps by putting tons of #if TARGET_OS_IPHONE in the code I figured I would be able to subclass both UIFont and NSFont using a common .h and just use separate .m files for each build. 尝试编写一些可在iOS和OS X之间使用的通用库。为了避免通过在代码中放入大量的#if TARGET_OS_IPHONE来弄乱应用程序,我认为我可以使用通用的.h和只需为每个构建使用单独的.m文件。 So I have the following subclass structure MyFont.h and MyFont_iOS.m and MyFont_OSX.m. 因此,我具有以下子类结构MyFont.h和MyFont_iOS.m和MyFont_OSX.m。 Each target includes the appropriate .m file. 每个目标都包含适当的.m文件。 Works fine on iOS but when executing on OSX I get an EXC_BAD_ACCCES creating the font object with a line like 在iOS上可以正常工作,但是在OSX上执行时,我得到一个EXC_BAD_ACCCES,它使用如下一行创建字体对象

MyFont *myFont = [MyFont fontWithName:@"Arial" size:132.0]; MyFont * myFont = [MyFont fontWithName:@“ Arial” size:132.0];

Can I not subclass NSFont? 我可以不继承NSFont吗?

Here's my basic code sample for the NSFont subclass (note as of this time I have no specialized code in the subclass. This is just a basic subclass) 这是我的NSFont子类的基本代码示例(请注意,到目前为止,该子类中没有专门的代码。这只是一个基本子类)

MyFont.h MyFont.h

#if TARGET_OS_IPHONE
@interface MyFont : UIFont
#else
@interface MyFont : NSFont
#endif
@end

MyFont_iOS.m MyFont_iOS.m

#import "MyFont.h"
@implementation MyFont
@end

MyFont_OSX.m MyFont_OSX.m

#import "MyFont.h"
@implementation MyFont
@end

Attempted Use of the sub classed font (in iOS App - Works Fine!) 尝试使用子字体(在iOS App中-很好!)

#import "MyFont.h"
...
MyFont *myFont = [MyFont fontWithName:@"Arial" size:45.0];

Attempted Use of the sub classed font (in OS X App - Crashes!) 尝试使用子类别的字体(在OS X App中-崩溃!)

#import "MyFont.h"
...
MyFont *myFont = [MyFont fontWithName:@"Arial" size:132.0];

FYI - I'm also getting the warning on this line 仅供参考-我也在这条线上收到警告

incompatible pointer types initializing 'MyFont *' with an expression of type 'NSFont *' 使用“ NSFont *”类型的表达式初始化“ MyFont *”的不兼容指针类型

So I'm definitely doing something wrong but just don't understand why it's forcing me to use NSFont but works fine on UIFont. 所以我绝对在做错事,但只是不明白为什么它迫使我使用NSFont,但在UIFont上却能正常工作。

Thanks for any guidance! 感谢您的指导!

fontWithName returns a NSFont/UIFont - fixed. fontWithName返回一个NSFont / UIFont-已修复。 IT cant be subclassed 不能归类

Even if it works, it is not defined behaviour 即使有效,也未定义行为


what you can do is have a wrapper that HOLDS the platform specific font 您可以做的是拥有一个可保留平台特定字体的包装器

eg 例如

@implementation MyFont {
    PlatformSpecificFont *_font
}
+ (instancetype)fontWithName:... {
    _font = [PlatformSpecificFont fontWithName:...];
    return self;
}

Wrapper is a good way to do this. 包装器是执行此操作的好方法。 An alternative: Depending on the target, either 另一种选择:根据目标,

#define MyFont NSFont

or 要么

#define MyFont UIFont

And where both require different code, you can add extensions to the class. 如果两者都需要不同的代码,则可以向该类添加扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM