开发者

Do NSMutableAttributedString addAttribute methods retain the passed in value?

For example is the following code memory safe?

NSMutableAttributedString *str = ...;

CTFontRef aFont = CTFontCreateWithName((CFStringRef)fontName, size, NULL);
[str addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
CFRelease(aFont);

Also, is CTFontCreateWithName efficient to call multiple times or should some effort be made to cache CTFontRef's for the same 开发者_如何学Cfont/size?


I believe it is safe to release the font object after adding it as an attribute. I have done so in my own Core Text code and never have any issues.

As for caching, it would make sense to keep a font object around if it will be used multiple times rather than releasing it and recreating it many times. Though, this is likely pre-optimisation, so I wouldn't make any conscious effort just yet. Profile it with your current code and decide whether or not the extra microseconds will be worth the work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜