iOS:如何使用Quartz测量字符串的宽度和高度? [英] iOS: How do you measure the width and height of a string using Quartz?

查看:126
本文介绍了iOS:如何使用Quartz测量字符串的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我问我的问题之前,这是来自Apple的文档re:如何使用Quartz确定字符串的宽度:

Before I ask my questions, this is from Apple's documentation re: how to determine the width of a string using Quartz:

如果文本测量对您的应用程序很重要,则有可能 使用Quartz 2D函数计算它们.但是,您可能首先 考虑使用ATSUI,其优势在于文本布局和 测量. ATSUI具有获取文本指标的多个功能.不是 仅可以获取布局后文本指标,但在极少数情况下 如果需要它们,则可以获得布局前的文本指标.不像 石英,您必须自己执行计算,ATSUI 为您计算测量结果.例如,您可以获得 通过调用ATSUI函数,将文本的图像边界矩形 ATSUMeasureTextImage.

If text measurements are important to your application, it is possible to calculate them using Quartz 2D functions. However, you might first consider using ATSUI, whose strength is in text layout and measurement. ATSUI has several functions that obtain text metrics. Not only can you obtain after-layout text metrics, but in the rare cases you need them, you can obtain before-layout text metrics. Unlike Quartz, for which you must perform the calculations yourself, ATSUI computes the measurements for you. For example, you can obtain the image bounding rectangle for text by calling the ATSUI function ATSUMeasureTextImage.

如果您确定Quartz文本比ATSUI更适合您的需求(或 可可),您可以按照以下步骤来测量文本的宽度 石英绘制它:

If you decide that Quartz text suits your needs better than ATSUI (or Cocoa), you can follow these steps to measure the width of text before Quartz draws it:

  1. 调用函数CGContextGetTextPosition以获取当前文本位置.
  2. 使用CGContextSetTextDrawingMode函数将文本绘制模式设置为kCGTextInvisible.
  3. 通过调用CGContextShowText函数在当前文本位置绘制文本来绘制文本.
  4. 通过调用CGContextGetTextPosition函数确定最终的文本位置.
  5. 从结束位置减去开始位置以确定文本的宽度.
  1. Call the function CGContextGetTextPosition to obtain the current text position.
  2. Set the text drawing mode to kCGTextInvisible using the function CGContextSetTextDrawingMode.
  3. Draw the text by calling the function CGContextShowText to draw the text at the current text position.
  4. Determine the final text position by calling the function CGContextGetTextPosition.
  5. Subtract the starting position from the ending position to determine the width of the text.

这是我的问题:

  1. 这真的是使用Core Graphics确定字符串宽度的最佳方法吗?由于我的文本与2D图形元素共存,因此似乎很脆弱,因此我想对所有渲染使用相同的上下文.我希望会有一些紧凑的方法,例如:

  1. Is this really the best way to determine the width of a string using Core Graphics? It seems flimsy and since my text co-exists with 2D graphical elements, I'd like to use the same context for all rendering. I was hoping there would be some compact method, like:

CGContextGetTextWidthAndHeight(context, text);

  • 我读到ATSUI已过时,将被Core Text取代.是真的,如果是的话,在iOS中是吗?

  • I read that ATSUI is outdated and going to be replaced by Core Text. Is that true and if so, is it in iOS?

    推荐答案

    在iPhone SDK上,NSString上有一系列提供所需内容的方法.

    On the iPhone SDK, there's a family of methods on NSString that provide what you want.

    从iOS 7.0开始,

    As of iOS 7.0, these methods are:

    - boundingRectWithSize:options:attributes:context:
    - sizeWithAttributes:
    

    在旧版本的iOS上,我们已弃用了这些软件:

    On older versions of iOS we had these, now deprecated:

    – sizeWithFont:  
    – sizeWithFont:forWidth:lineBreakMode:  
    – sizeWithFont:constrainedToSize:  
    – sizeWithFont:constrainedToSize:lineBreakMode:  
    – sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
    

    这篇关于iOS:如何使用Quartz测量字符串的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆