如何找出 UILabel 自动换行的时间 [英] How to find out when UILabel wordwraps

查看:66
本文介绍了如何找出 UILabel 自动换行的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的 tableViewController 创建了几个不同的 UITableViewCell,这是因为我想根据返回的内容显示不同的数据位.因此,每个 UITableViewCell 的高度都不同.

I have created a couple of different UITableViewCells for my tableViewController, this is because I have different bits of data I want to display depending what comes back. Because of this each UITableViewCell is of a different height.

这一切都很好,但是我的一个 UILabel 必须显示一个很大的 NSString,大到我不得不将它换行到第二行,这里唯一的问题是它搞乱了我的 UITableViewCell 格式.

This is all fine, However one of my UILabels has to display a large NSString, so large that I have had to word wrap it onto a second line, The only issue here is that it messes up my UITableViewCell formatting.

我的问题是,当 UILabel 收到 NSString 必须使用 wordrap 时,是否可以标记或捕获某种类型的操作,因为 的大小NSString 太长?

My question is, is it possible to flag or capture some type of action when a UILabel receiving a NSString has to use wordrap because the size of the NSString is too long?

或者有没有办法计算 NSString 的物理长度(不是字符串中有多少个字符),所以我可以决定嘿我需要重新格式化我的 UITableViewCell..

Or is there a way to calculate the physical length (not how many chars are in the string) of the NSString so I can decide hey I need to reformat my UITableViewCell..

任何帮助将不胜感激.

推荐答案

通常的方法是使用 sizeWithFont:constrainedToSize:lineBreakMode: 来获取字符串的大小.你像这样使用它:

The usual way is to use sizeWithFont:constrainedToSize:lineBreakMode: to get the size of a string. You use it like this:

CGSize stringSize = [theString sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:12] constrainedToSize:CGSizeMake(320, 3000) lineBreakMode:NSLineBreakByWordWrapping ];

这是为表格单元格的整个宽度设置的,但您可能希望使用标签的宽度.

This is set for the full width of a table cell, but you would want to use the width of your label instead.

这篇关于如何找出 UILabel 自动换行的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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