objective-c – Cocoa从字体中获取字符串宽度(以像素为单位)

objective-c – Cocoa从字体中获取字符串宽度(以像素为单位),第1张

概述嘿伙计们,我试图从字体和字体大小中找到字符串宽度(以像素为单位).我目前正在使用此代码,但它不是100%的工作时间.还有另一种方法吗? NSSize textSize = [aTextLayer.string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:@"Bank Gothic Medium", NSFontNam 嘿伙计们,我试图从字体和字体大小中找到字符串的宽度(以像素为单位).我目前正在使用此代码,但它不是100%的工作时间.还有另一种方法吗?

NSSize textSize = [aTextLayer.string sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:@"Bank Gothic Medium",NSFontNameattribute,[NSNumber numberWithfloat:aTextLayer.FontSize],NSFontSizeAttribute,nil]];
解决方法 通过Application Kit Additions为NSAttributedString授予-size方法.

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:    @"Bank Gothic Medium",nil];NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:aTextLayer.string attributes:attributes];NSSize size = attributedString.size;
总结

以上是内存溢出为你收集整理的objective-c – Cocoa从字体中获取字符串宽度(以像素为单位)全部内容,希望文章能够帮你解决objective-c – Cocoa从字体中获取字符串宽度(以像素为单位)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1007530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存