UITextView:获取带有换行信息的文本

是否有可能获得UITextView的文本内的包装信息。

在这里输入图像说明

所以在这种情况下。 我将得到像“亲爱的StackOverFlow,\ n \ n你没有更多的\ n”的文字。 我想在“现在我”之后得到一个换行符,就像在UITextView中所显示的那样。

在这里看到我的答案:

https://stackoverflow.com/a/13588322/341994

你要问的是Core Text为你做的。 事实上,核心文本是UITextView如何知道如何包装文本。 所以你可以问Core Text在哪里换行,就像UITextView一样。 看到我的答案中的示例代码 – 这比你想要做的更简单也更可靠。

编辑:上面的马特的答案提供了一个更直接的方式做到这一点。

好吧,这似乎是不可能的。 我必须手动完成。

这可能不是很准确,我仍然在testing错误。

- (NSString*) wrappedStringForString: (NSString*)rawString { NSString *resultSring = [NSString stringWithFormat:@""]; float textViewWidth = 130; //Width of the UITextView //Check if already small. CGSize textSize = [rawString sizeWithFont:self.backMessageTextView.font]; float textWidth = textSize.width; if (textWidth < textViewWidth) { return rawString; } //Loop NSUInteger length = [rawString length]; unichar buffer[length]; [rawString getCharacters:buffer range:NSMakeRange(0, length)]; NSString *singleLine = [NSString stringWithFormat:@""]; NSString *word = [NSString stringWithFormat:@""]; NSString *longWord = [NSString stringWithFormat:@""]; float difference; for (NSUInteger i = 0; i < length; i++) { unichar character = buffer[i]; //Add to word if (character != '\n') { word = [NSString stringWithFormat:@"%@%c", word, character]; } if (character == '\n') { float wordLength = [word sizeWithFont:self.backMessageTextView.font].width; float lineLength = [singleLine sizeWithFont:self.backMessageTextView.font].width; if ((lineLength + wordLength) > textViewWidth) { resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; singleLine = @""; singleLine = [singleLine stringByAppendingFormat:@"%@\n",word]; word = @""; } else { singleLine = [singleLine stringByAppendingString: word]; word = @""; resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; singleLine = @""; } } else if (character == ' ') { float wordLength = [word sizeWithFont:self.backMessageTextView.font].width; float lineLength = [singleLine sizeWithFont:self.backMessageTextView.font].width; if ((lineLength + wordLength) > textViewWidth) { if (wordLength > textWidth) { resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; singleLine = @""; int j = 0; for (; j < [word length]; j++) { unichar longChar = [word characterAtIndex:j]; longWord = [NSString stringWithFormat:@"%@%c", longWord, longChar]; float longwordLength = [longWord sizeWithFont:self.backMessageTextView.font].width; float longlineLength = [singleLine sizeWithFont:self.backMessageTextView.font].width; if ((longlineLength + longwordLength) >= textViewWidth) { singleLine = [singleLine stringByAppendingString:longWord]; word = @""; longWord = @""; break; } } } resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; singleLine = @""; } singleLine = [singleLine stringByAppendingString: word]; word = @""; } } float wordLength = [word sizeWithFont:self.backMessageTextView.font].width; float lineLength = [singleLine sizeWithFont:self.backMessageTextView.font].width; // handle any extra chars in current word if (wordLength > 0) { if ((lineLength + wordLength) > textViewWidth) { resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; singleLine = @""; } singleLine = [singleLine stringByAppendingString:word]; } // handle extra line if (lineLength > 0) { resultSring = [resultSring stringByAppendingFormat:@"%@\n", singleLine]; } return resultSring; }