是否可以将UIFont子类并使用另一个子类(或字体描述符)初始化它?问题是,我不能调用super.init(descriptor:size:)进行初始化,因为它是一个方便的初始化程序。这样做的目的是更改ascender和descender的属性(覆盖只读属性),因为我只显示数字,字母上方和下面的空格太多(我直接在图形上下文中绘制)。如果有另一个优雅的解决方案,那将是非常受欢迎的。
我很感谢你的帮助,谢谢。
发布于 2015-09-24 22:45:37
您所要求的是中断封装,也许也没有意义。
实现这一目标的一种更整洁的方法不是使用继承,而是通过一些假设,向类别添加一个UIFont,并提供您自己的方便工厂(例如类)方法。
@interface UIFont (MyUIFont)
@property (assign) CGFloat ascender;
@property (assign) CGFloat descender;
+(instancetype) UIFontWithLessSpacing;
@end
@implementation UIFont (MyUIFont)
+(instancetype) UIFontWithLessSpacing
{
UIFont *font = [UIFontWIthName:@"MyFont" size:12.0f];
font.ascender = 0.0;
font.descender = 0.0;
return font;
}有一些注意事项:
UIFont实际上响应了选择器setAscender和setDescender -它们隐藏在公共接口中。https://stackoverflow.com/questions/32771877
复制相似问题