sizeWithFont:constrainedToSize: не учитывает \n в NSString

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *substring = textView.text;
    substring = (text.length == 0 && range.length == 1) ?
    [substring substringToIndex: substring.length-1] : [substring stringByAppendingString: text];

    CGSize size = [substring sizeWithFont:textView.font
                        constrainedToSize:CGSizeMake(textView.frame.size.width, 200.0)];

    CGFloat height = MAX(size.height, firstHeight);

    if(height != lastHeight)
    {
        CGFloat y = height - lastHeight;
        CHANGE_HEIGHT(textView, height);
        lastHeight = height;
    }   
    return YES;
}

Проблема в том, что sizeWithFont:constrainedToSize: дает мне неправильный рост. Например, если текст "Hello \n", sizeWithFont не будет учитывать "\n", поэтому Hello" и "Hello \n"< /strong> тексты имеют одинаковую высоту.

Любая необходимая помощь будет оценена!


person PizzaByte    schedule 21.05.2014    source источник
comment
А если добавить пробел после \n? Привет\n   -  person nerowolfe    schedule 21.05.2014
comment
Спасибо! Теперь работает :)   -  person PizzaByte    schedule 21.05.2014


Ответы (1)


Не связано, но то, как вы получаете свой substring, игнорирует диапазон, в котором изменяется текст.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *textToMeasure = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if ([textToMeasure hasSuffix:@"\n"]) {
        textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
    ...
}
person Jkmn    schedule 21.05.2014