计算 UITextView 中的行数,按帧大小包装的行 [英] Counting the number of lines in a UITextView, lines wrapped by frame size

查看:21
本文介绍了计算 UITextView 中的行数,按帧大小包装的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当文本被文本视图的框架包裹时,是否有任何分隔符可以识别文本是否被包裹.

I wanted to know when a text is wrapped by the frame of the text view is there any delimiter with which we can identify whether the text is wrapped or not.

例如,如果我的文本视图的宽度为 50 像素并且文本超过了该宽度,则它会将文本换行到下一行.

For instance if my text view has a width of 50 px and text is exceeding that, it wraps the text to next line.

我想计算我的文本视图中的行数.现在 " " 和 " " 对我没有帮助.

I wanted to count the number of lines in my text view. Now " " and " " are not helping me.

我的代码是:

NSCharacterSet *aCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"

"];
    NSArray *myArray = [textViewText componentsSeparatedByCharactersInSet:aCharacterSet];
    NSLog(@"%d",[myArray count]);

推荐答案

这种变化考虑了你如何换行和 UITextView 的最大尺寸,并可能输出更精确的高度.例如,如果文本不适合,它将被截断为可见大小,如果您将整个单词(这是默认设置)换行,则可能会导致比其他情况下更多的行.

This variation takes into account how you wrap your lines and the max size of the UITextView, and may output a more precise height. For example, if the text doesn't fit it will truncate to the visible size, and if you wrap whole words (which is the default) it may result in more lines than if you do otherwise.

UIFont *font = [UIFont boldSystemFontOfSize:11.0];
CGSize size = [string sizeWithFont:font 
                      constrainedToSize:myUITextView.frame.size 
                      lineBreakMode:UILineBreakModeWordWrap]; // default mode
float numberOfLines = size.height / font.lineHeight;

这篇关于计算 UITextView 中的行数,按帧大小包装的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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