-[NSString sizeWithFont:forWidth:lineBreakMode:] 有什么用? [英] What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?

查看:23
本文介绍了-[NSString sizeWithFont:forWidth:lineBreakMode:] 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的问题我如何获得 -[NSString sizeWithFont:forWidth:lineBreakMode:] 工作?",我了解到 -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] 实际上是我需要的.

In my question "How do I get -[NSString sizeWithFont:forWidth:lineBreakMode:] to work?", I learned that -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] was actually what I needed.

-[NSString sizeWithFont:forWidth:lineBreakMode:] 的文档说明它实际上并没有将文本换行到其他行.那么我将如何使用它?(示例会有所帮助.)

The documentation for -[NSString sizeWithFont:forWidth:lineBreakMode:] states explains it doesn't actually wrap the text to additional lines. So how would I use it? (Examples would help.)

推荐答案

它也适用于多行字符串.

It works great for multi line strings as well.

基本上,它是一个函数,可让您查看使用字体和换行符模式渲染时字符串的大小.

Basically its a function that lets you see how big a string is going to be when rendered with a font and line break mode.

当我想在某个区域显示可变长度的文本时,我会在应用程序的几个地方使用它.

I use it in a few spots in my application when I have variable length text I want to display in a certain area.

默认情况下,UILabel 会将文本垂直居中.要使文本顶部对齐,您需要将标签大小调整为其中的字符串所需的高度.

By default a UILabel will center the text vertically. To have the text top aligned you need to size the label to be only the height required by the string that's in it.

我使用这种方法来做到这一点.

I use this method to do this.

我将如何使用它来做到这一点的示例如下:

And example of how I would use it to do that is as follows:

//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                        constrainedToSize:maximumLabelSize 
                        lineBreakMode:yourLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

您指定要放置文本的区域有多大,然后此方法会告诉您它将占用多少空间(根据需要换行).此外,如果字符串会溢出您提供的矩形的边界,您可以判断并决定如何显示文本.

You specify how big of an area you have to put the text in, and then this method tells you how much space it will take up (wrapping as required). Also if the string will overflow the bounds of the rect you provide you can tell and then decided how to display the text.

对它的引用实际上并未包装文本,因为此方法实际上并未对文本执行任何操作.它只是虚拟地进行布局,并返回实际布局所需的区域大小.

The reference to it not actually wrapping the text is there because this method doesn't actually do anything to the text. It just virtually lays it out and returns how big of an area it would need to really lay it out.

标签(或您用于文本的任何容器)负责执行包装/其他需要完成的工作.

its the responsibility of the label (or what ever container you are using for the text) to perform the wrapping/what ever else that needs to be done.

希望有所帮助.

克里斯.

这篇关于-[NSString sizeWithFont:forWidth:lineBreakMode:] 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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