如何读取UITextView中的行数 [英] How to Read Number of lines in UITextView

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

问题描述

我使用 UITextView 在我的视图中,我要求计数由textview am包含的行数,使用以下函数读取'\ n'。然而,这只有当从键盘按下返回键,但在case warapper(当我键入连续字符,我不会得到新的行字符)。我如何读取新的字符,当线更改,而没有击中返回键?任何人都不知道如何..请共享它..
我以下这个链接链接

I am using UITextView In my View , I have requirement to count number of line contained by textview am using following function to read '\n' . However this works only when return key is pressed from keyboard , but in case line warapper (when i type continuous characters i wont get new line char ) . How do i read new char when line is changed without hitting return key ?? Anybody has nay idea how to .. please share it .. I am follwing this link Link

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"\n"]) {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];

        // Return NO so that the final '\n' character doesn't get added
        return NO;
    }
    // For any other character return YES so that the text gets added to the view
    return YES;
}


推荐答案

属性的UITextView获得文本的高度,以像素为单位,除以行高度间距的UITextView的字体,以获得文本行的总数UIScrollView(开和关屏幕),包括包裹和线断文本。

You can look at the contentSize property of your UITextView to get the height of the text in pixels, and divide by the line height spacing of the UITextView's font to get the number of text lines in the total UIScrollView (on and off screen), including both wrapped and line broken text.

这篇关于如何读取UITextView中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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