【问题标题】:Detect new-line character for auto enter new line text on UITextView on iOS检测换行符以在 iOS 上的 UITextView 上自动输入换行文本
【发布时间】:2014-12-16 20:09:54
【问题描述】:

iOS 应用在 UITextView 上打字时,如果文字超出 UITextView 的宽度,UITextView 会自动换行继续打字,但问题是在取出文字时,还是只有一行文字。

但是当我从这个文本视图中获取文本时

NSString* newtext = textview.text;

newtext 的值将是“AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOMMM”(都是单行),但我预计它将是“AAAAAAAAAAAAAAAAAAAAAAAAAAAAO\nMMM”(注意“\n”字符通知新行)

有什么办法吗

【问题讨论】:

  • 它不会自动输入换行符,它只是环绕...您是说您想要 textview.text 的字符串表示形式能够反映换行符?
  • @LyndseyScott,是的,这是我的愿望

标签: ios uitextview newline


【解决方案1】:

UITextView 在其文本到达行尾时不会自动输入换行符——它只是用换行符换行。但是,如果您想要 UITextView 文本的字符串表示形式,其中包含用于指示各种换行符的换行符,请尝试以下操作:

// This method takes in the `UITextView` and returns the string
// representation which includes the newline characters
- (NSString*)textViewWithNewLines:(UITextView*)textView {

    // Create a variable to store the new string
    NSString *stringWithNewlines = @"";

    // Get the height of line one and store it in
    // a variable representing the height of the current
    // line
    int currentLineHeight = textView.font.lineHeight;

    // Go through the text view character by character
    for (int i = 0 ; i < textView.text.length ; i ++) {

        // Place the cursor at the current character
        textView.selectedRange = NSMakeRange(i, 0);

        // And use the cursor position to help calculate
        // the current line height within the text view
        CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;

        // If the y value of the cursor is greater than
        // the currentLineHeight, we've moved onto the next line
        if (cursorPosition.y > currentLineHeight) {

            // Increment the currentLineHeight such that it's
            // set to the height of the next line
            currentLineHeight += textView.font.lineHeight;

            // If there isn't a user inputted newline already,
            // add a newline character to reflect the new line.
            if (textView.text.length > i - 1 &&
                [textView.text characterAtIndex:i-1] != '\n') {
                stringWithNewlines = [stringWithNewlines stringByAppendingString:@"\n"];
            }

            // Then add the character to the stringWithNewlines variable
            stringWithNewlines = [stringWithNewlines stringByAppendingString:[textView.text substringWithRange:NSMakeRange(i, 1)]];
        } else {

            // If the character is still on the "current line" simply
            // add the character to the stringWithNewlines variable
            stringWithNewlines = [stringWithNewlines stringByAppendingString:[textView.text substringWithRange:NSMakeRange(i, 1)]];
        }
    }

    // Return the string representation of the text view
    // now containing the newlines
    return stringWithNewlines;
}

【讨论】:

  • 感谢@LyndseyScott,它就像一个魅力。但是我想知道cursorPosition到底是什么,是我们输入文字时的光标吗?
  • @pf2707 是的,它是一个变量,指示光标位置,如上一行使用 textView.selectedRange 设置的那样。我将光标位置用作某种移动的“作弊”,以帮助找到每个字符的位置。
  • 我知道了,有时我们需要一些作弊来完成目标,谢谢你的好主意,它真的救了我
  • @pf2707 没问题 :)
  • @LyndseyScott 您知道适用于字符串的类似方法吗?还是我应该只使用文本视图?谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 2013-11-29
  • 2017-04-10
  • 1970-01-01
相关资源
最近更新 更多