Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

When the number of text lines excels the maxNumberOfLines, the text view will scroll the top then bottom quickly, just like a flash #98

Open
shayanbo opened this issue Jul 24, 2014 · 8 comments

Comments

@shayanbo
Copy link

When the number of text lines excels the maxNumberOfLines, the text view will scroll the top then bottom quickly, just like a flash

@shayanbo shayanbo changed the title When the number of text lines excel the maxNumberOfLines, the text view will scroll the top then bottom When the number of text lines excels the maxNumberOfLines, the text view will scroll the top then bottom quickly, just like a flash Jul 24, 2014
@rajatk
Copy link

rajatk commented Aug 6, 2014

i'm seeing this issue. It's especially prominent when using in tableviewcell. on reload, the textview shows top part for a quick second then jumps to bottom.

@HaloZero
Copy link

If you want a quick fix, you can turn off animation.

@Shahar2k5
Copy link

@HaloZero , i turned the animation off like that:
messageField.animateHeightChange = NO; //turns off animation
but still....
anything else i need to do?

Tx.

@HaloZero
Copy link

I don't believe so, are you still getting the bug?

@Shahar2k5
Copy link

Yeah, still the same.
On the simulator, work OK.
On the iPad the bug exist.

@tsaisean
Copy link

tsaisean commented Dec 3, 2014

I found this issue on iPhone6/6+, too.
The HPTextViewInternal.contentSize.height behaves strangely.

@truculent
Copy link

fix this method with :

   - (CGFloat)measureHeight
   {
      if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
      {
        // This is the code for iOS 7. contentSize no longer returns the correct value, so
        // we have to calculate it.
        //
        // This is partly borrowed from HPGrowingTextView, but I've replaced the
        // magic fudge factors with the calculated values (having worked out where
        // they came from)

        CGRect frame = self.internalTextView.bounds;

        // Take account of the padding added around the text.

        UIEdgeInsets textContainerInsets = self.internalTextView.textContainerInset;
        UIEdgeInsets contentInsets = self.internalTextView.contentInset;

        CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + self.internalTextView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
        CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;

        frame.size.width -= leftRightPadding;
        frame.size.height -= topBottomPadding;

        NSString *textToMeasure = self.internalTextView.text;
        if ([textToMeasure hasSuffix:@"\n"])
        {
            textToMeasure = [NSString stringWithFormat:@"%@-", self.internalTextView.text];
        }

        // NSString class method: boundingRectWithSize:options:attributes:context is
        // available only on ios7.0 sdk.

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];

        NSDictionary *attributes = @{ NSFontAttributeName: self.font, NSParagraphStyleAttributeName : paragraphStyle };

        CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:attributes
                                                  context:nil];

        CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
        return measuredHeight;
       }
       else
       {
           return self.internalTextView.contentSize.height;
       }
   }

@ghost
Copy link

ghost commented Apr 24, 2015

thx truculent! Great fix.

mikeleo added a commit to CodewareTechnology/GrowingTextView that referenced this issue May 6, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants