开发者

UITextView : get text with wrap info

开发者 https://www.devze.com 2023-04-10 05:20 出处:网络
Is it possible to get the text inside a UITextView with its wrap info. 开发者_如何学JAVA So in this case. I will get text like \"Dear StackOverFlow,\\n\\nYou make my...\" with no more \"\\n\". I wou

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

开发者_如何学JAVA

UITextView : get text with wrap info

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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号