UITextView : get text with wrap info

后端 未结 2 1668
花落未央
花落未央 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

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

提交回复
热议问题