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

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

问题描述

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


如果文本测量对您的应用程序很重要,使用Quartz 2D函数计算它们可能需要
。但是,您可能首先考虑使用ATSUI
,其强度在于文本布局和
测量。 ATSUI有几个获取文本度量的函数。不是
只能获得布局后文本指标,但在极少数情况下需要
,您可以获得布局前文本指标。与
Quartz不同,您必须自己执行计算,ATSUI
为您计算测量值。例如,您可以通过调用ATSUI函数
ATSUMeasureTextImage来获取文本的
图像边界矩形。



如果您认为Quartz文本适合您的需要比ATSUI(或
Cocoa)更好,你可以按照以下步骤来测量
之前的文本宽度Quartz绘制它:


  1. 调用函数CGContextGetTextPosition以获取当前文本位置。

  2. 使用函数CGContextSetTextDrawingMode将文本绘制模式设置为kCGTextInvisible。

  3. 通过调用函数CGContextShowText绘制文本以在当前文本位置绘制文本。

  4. 通过调用函数CGContextGetTextPosition确定最终文本位置。

  5. 从结束位置减去起始位置以确定文本的宽度。


这里有我的问题:


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



    CGContextGetTextWidthAndHeight(context,text);


  2. <我读到ATSUI已经过时,将被Core Text取代。这是真的,如果是这样,它是否在iPhone SDK中?



解决方案

在iPhone SDK上, NSString 上有一系列方法可以提供你想要的东西。



从iOS开始7.0,这些方法是

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

在旧版本的iOS上我们有这些,现已弃用:

   -  sizeWithFont:
- sizeWithFont:forWidth:lineBreakMode:
- sizeWithFont:constrainedToSize:
- sizeWithFont:constrainedToSize:lineBreakMode:
- sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:


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

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.

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. 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.

Here are my questions:

  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);

  2. I read that ATSUI is outdated and going to be replaced by Core Text. Is that true and if so, is it in the iPhone SDK?

解决方案

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

As of iOS 7.0, these methods are:

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

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

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

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

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