代码之家  ›  专栏  ›  技术社区  ›  Christos Hayward

在iOS中,为您提供的TTF获取UIFont的当前方式是什么?

  •  0
  • Christos Hayward  · 技术社区  · 11 年前

    从…起 https://developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html#//apple_ref/occ/instm/NSString/sizeWithAttributes ::

    UITextAttributeFont
    Key to the font in a text attributes dictionary.
    The corresponding value is an instance of UIFont.
    Use a font with size 0.0 to get the default font size for the current context.
    Available in iOS 5.0 and later.
    Deprecated in iOS 7.0.
    Declared in UIStringDrawing.h.
    

    我正在尝试获得特定单行文本字符串的宽度(也需要高度),这就是我现在所拥有的,“…”是我适当初始化字体所需要的。该方法是不完整的(它不返回值),但我希望使用一种不推荐使用的方法来指定字体的信息,并返回如果绘制字符串将有多少像素宽。

    -(float)getLength:(NSString *)text
    {
        CGSize size = [text sizeWithAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:...];
    }
    

    语义是否从“在进行最后调用时指定字体详细信息”更改为“指定一次字体详细信息,它们将一直有效,直到您将其更改为其他内容”?

    谢谢

    1 回复  |  直到 11 年前
        1
  •  1
  •   Community CDub    7 年前

    就在几天前,我在自己的工作中看到了一个与你的问题非常接近的问题。

    你要找的电话(这将解决你的最终问题)是 " boundingRectWithSize: options: attributes: context: " ,你经过的地方 UIFont you loaded (from your TTF file) 作为属性之一。

    更好的是,使用NSAttributedString或NSMutableAttributedString(它也有 "boundingRectWithSize: options: context: " method ,因为这样您就可以混合使用字体和字体样式来计算文本的宽度和高度。