UITextView : get text with wrap info

后端 未结 2 1669
花落未央
花落未央 2021-01-23 12:04

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

\"enter

So

相关标签:
2条回答
  • 2021-01-23 12:36

    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.

    0 讨论(0)
  • 2021-01-23 12:36

    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 讨论(0)
提交回复
热议问题