0
我有一個在ARC(DTCoreText)上運行的項目,我想在UIFont上實現一個返回匹配的CTFontRef的類別方法。這是我到目前爲止:如何命名和處理返回CFType的函數的內存管理
@implementation UIFont (DTCoreText)
+ (UIFont *)fontWithCTFont:(CTFontRef)ctFont
{
NSString *fontName = (__bridge_transfer NSString *)CTFontCopyName(ctFont, kCTFontPostScriptNameKey);
CGFloat fontSize = CTFontGetSize(ctFont);
return [UIFont fontWithName:fontName size:fontSize];
}
- (CTFontRef)CTFont
{
CTFontRef newFont = CTFontCreateWithName((__bridge CFStringRef)self.fontName, self.pointSize, NULL);
return newFont;
}
@end
從技術上講,由於沒有自動釋放CF對象,所以返回+1引用。
在我的代碼,我稱它是這樣的:
- (void)replaceFont:(UIFont *)font inRange:(NSRange)range
{
[self beginEditing];
[self removeAttribute:(id)kCTFontAttributeName range:range];
CTFontRef ctFont = [font CTFont];
[self addAttribute:(id)kCTFontAttributeName value:CFBridgingRelease(ctFont) range:range];
[self endEditing];
}
這個不用分析,因爲CFBridgingRelease的警告,但我擔心的是需要一種不從的名字被明確給別人類別方法。
不同的建議一直使用C函數用於創建,而不是:
static CTFontRef CTFontCreateFromUIFont(UIFont *font)
這會更明顯,因爲的創建名稱告訴他是得到一個開發商+1參考。
那麼你認爲什麼是「正確」的方式?還有其他建議嗎?