测量字符串的像素宽度 [英] Measuring the pixel width of a string

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

问题描述

我需要在Cocoa Touch中测量字符串的像素宽度。

I need to measure the pixel width of a string in Cocoa Touch. Can anyone point me to a link that explains how to do this?

推荐答案

在iPhone操作系统上,它略有不同,而是看看 NSString UIKit添加参考

On iPhone OS it is slightly different, instead look at the NSString UIKit Additions Reference. The idea is the same as in Cocoa for Mac OS X, but there are more methods.

对于单行文本,使用:

- (CGSize)sizeWithFont:(UIFont *)font 
              forWidth:(CGFloat)width 
         lineBreakMode:(UILineBreakMode)lineBreakMode

对于多行文本,使用:

- (CGSize)sizeWithFont:(UIFont *)font 
     constrainedToSize:(CGSize)size 
         lineBreakMode:(UILineBreakMode)lineBreakMode

使用 UILineBreakMode 作为单行文本的参数可能会引起混淆,但这是因为行break也用于定义如何截断文本。

The use of a UILineBreakMode as argument for single lines of text can be confusing, but this is because the line break is also used to define how to truncate the text.

这篇关于测量字符串的像素宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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