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

发布于 2024-12-07 15:51:15 字数 214 浏览 12 评论 0原文

是否可以获取 UITextView 中的文本及其换行信息。

在此处输入图像描述

在这种情况下。我会收到类似“亲爱的 StackOverFlow,\n\n你让我的...”这样的文本,而不再有“\n”。我想在“and now I”之后得到一个换行符,如 UITextView 中所示。

Is it possible to get the text inside a UITextView with its wrap info.

enter image description here

So in this case. I will get text like "Dear StackOverFlow,\n\nYou make my..." with no more "\n". I would like to get a newline after "and now I", like shown in the UITextView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清欢 2024-12-14 15:51:15

在这里查看我的答案:

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

您要求做的正是核心文本为您做什么。事实上,Core Text 是 UITextView 如何知道如何换行的。所以你可以询问 Core Text 换行符在哪里,就像 UITextView 一样。请参阅我的答案中的示例代码 - 它比您想要做的更简单、更可靠。

See my answer here:

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

What you are asking to do is exactly what Core Text does for you. Indeed, Core Text is how UITextView knows how to wrap text. So you can ask Core Text where the line breaks are, just as UITextView does. See the example code in my answer - it's a lot simpler and more reliable than what you're trying to do.

野心澎湃 2024-12-14 15:51:15

编辑:
马特上面的回答提供了一种更直接的方法来做到这一点。

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

这可能不是很准确,我仍在测试错误。

- (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;
}

Edit :
Matt's answer above provides a more straight forward way of doing this.

Ok it seems this is not possible. I had to do it manually.

This may not be very accurate and I am still testing for bugs.

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文