是否可以使用其换行信息在UITextView中获取文本。
所以在这种情况下。我会得到像“亲爱的StackOverFlow,\ n \ n你制作我的......”这样的文字而不再是“\ n”。我希望在“现在我”之后得到换行符,如UITextView中所示。
答案 0 :(得分:5)
请在此处查看我的回答:
https://stackoverflow.com/a/13588322/341994
您要求做的就是Core Text为您所做的一切。实际上,Core Text是如何 UITextView知道如何包装文本。因此,您可以向Core Text询问换行符的位置,就像UITextView一样。请参阅我的答案中的示例代码 - 它比您尝试的更简单,更可靠。
答案 1 :(得分:-1)
修改: 马特在上面的回答提供了一种更直接的方法。
好吧,似乎这是不可能的。我必须手动完成。
这可能不太准确,我仍在测试错误。
- (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;
}