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

fix the issue about measurement of content size in iOS7.1 #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions README.md

This file was deleted.

90 changes: 40 additions & 50 deletions class/HPGrowingTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ @implementation HPGrowingTextView
@synthesize minHeight;
@synthesize font;
@synthesize textColor;
@synthesize textAlignment;
@synthesize textAlignment;
@synthesize selectedRange;
@synthesize editable;
@synthesize dataDetectorTypes;
Expand Down Expand Up @@ -88,15 +88,17 @@ -(void)commonInitialiser
CGRect r = self.frame;
r.origin.y = 0;
r.origin.x = 0;
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
internalTextView = [[HPTextViewInternal alloc] initWithFrame:r textContainer:textContainer];
#else
internalTextView = [[HPTextViewInternal alloc] initWithFrame:r];
#endif

if ([internalTextView respondsToSelector:@selector(initWithFrame:textContainer:)]){
internalTextView = [[HPTextViewInternal alloc] initWithFrame:r textContainer:textContainer];
}else{
internalTextView = [[HPTextViewInternal alloc] initWithFrame:r];
}

internalTextView.delegate = self;
internalTextView.scrollEnabled = NO;
internalTextView.font = [UIFont fontWithName:@"Helvetica" size:13];
internalTextView.contentInset = UIEdgeInsetsZero;
internalTextView.font = [UIFont fontWithName:@"Helvetica" size:13];
internalTextView.contentInset = UIEdgeInsetsZero;
internalTextView.showsHorizontalScrollIndicator = NO;
internalTextView.text = @"-";
internalTextView.contentMode = UIViewContentModeRedraw;
Expand All @@ -111,7 +113,7 @@ -(void)commonInitialiser
internalTextView.text = @"";

[self setMaxNumberOfLines:3];

[self setPlaceholderColor:[UIColor lightGrayColor]];
internalTextView.displayPlaceHolder = YES;
}
Expand Down Expand Up @@ -196,7 +198,7 @@ - (void)setMaxHeight:(int)height
-(void)setMinNumberOfLines:(int)m
{
if(m == 0 && minHeight > 0) return; // the user specified a minHeight themselves.

// Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
NSString *saveText = internalTextView.text, *newText = @"-";

Expand Down Expand Up @@ -246,7 +248,7 @@ - (UIColor *)placeholderColor
return internalTextView.placeholderColor;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
[internalTextView setPlaceholderColor:placeholderColor];
}
Expand Down Expand Up @@ -291,13 +293,13 @@ - (void)refreshHeight

if ([UIView resolveClassMethod:@selector(animateWithDuration:animations:)]) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
[UIView animateWithDuration:animationDuration
delay:0
[UIView animateWithDuration:animationDuration
delay:0
options:(UIViewAnimationOptionAllowUserInteraction|
UIViewAnimationOptionBeginFromCurrentState)
UIViewAnimationOptionBeginFromCurrentState)
animations:^(void) {
[self resizeTextView:newSizeH];
}
}
completion:^(BOOL finished) {
if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:newSizeH];
Expand All @@ -314,13 +316,13 @@ - (void)refreshHeight
[UIView commitAnimations];
}
} else {
[self resizeTextView:newSizeH];
[self resizeTextView:newSizeH];
// [fixed] The growingTextView:didChangeHeight: delegate method was not called at all when not animating height changes.
// thanks to Gwynne <http://blog.darkrainfall.org/>

if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:newSizeH];
}
}
}
}
}
Expand All @@ -333,13 +335,6 @@ - (void)refreshHeight
[internalTextView setNeedsDisplay];
}


// scroll to caret (needed on iOS7)
if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
{
[self performSelector:@selector(resetScrollPositionForIOS7) withObject:nil afterDelay:0.1f];
}

// Tell the delegate that the text view changed
if ([delegate respondsToSelector:@selector(growingTextViewDidChange:)]) {
[delegate growingTextViewDidChange:self];
Expand All @@ -349,23 +344,24 @@ - (void)refreshHeight
// Code from apple developer forum - @Steve Krulewitz, @Mark Marszal, @Eric Silverberg
- (CGFloat)measureHeight
{
if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])

if (sizeof(void*) == 4)
{
return ceilf([self.internalTextView sizeThatFits:self.internalTextView.frame.size].height);
// Executing in a 32-bit environment
return [self.internalTextView sizeThatFits:CGSizeMake(self.internalTextView.frame.size.width, CGFLOAT_MAX)].height-(self.internalTextView.contentInset.top+self.internalTextView.contentInset.bottom);
}
else {
return self.internalTextView.contentSize.height;
//64-bit 7.1 later
if([[UIDevice currentDevice].systemVersion floatValue] >= 7.1){
return ceilf([self.internalTextView sizeThatFits:self.internalTextView.frame.size].height);
}
else{
//64-bit 7.0
return [self.internalTextView sizeThatFits:CGSizeMake(self.internalTextView.frame.size.width, CGFLOAT_MAX)].height-(self.internalTextView.contentInset.top+self.internalTextView.contentInset.bottom);
}
}
}

- (void)resetScrollPositionForIOS7
{
CGRect r = [internalTextView caretRectForPosition:internalTextView.selectedTextRange.end];
CGFloat caretY = MAX(r.origin.y - internalTextView.frame.size.height + r.size.height + 8, 0);
if (internalTextView.contentOffset.y < caretY && r.origin.y != INFINITY)
internalTextView.contentOffset = CGPointMake(0, caretY);
}

-(void)resizeTextView:(NSInteger)newSizeH
{
if ([delegate respondsToSelector:@selector(growingTextView:willChangeHeight:)]) {
Expand All @@ -384,12 +380,6 @@ -(void)resizeTextView:(NSInteger)newSizeH

- (void)growDidStop
{
// scroll to caret (needed on iOS7)
if ([self respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
{
[self resetScrollPositionForIOS7];
}

if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
[delegate growingTextView:self didChangeHeight:self.frame.size.height];
}
Expand All @@ -414,7 +404,7 @@ -(BOOL)resignFirstResponder

-(BOOL)isFirstResponder
{
return [self.internalTextView isFirstResponder];
return [self.internalTextView isFirstResponder];
}


Expand Down Expand Up @@ -450,7 +440,7 @@ -(void)setFont:(UIFont *)afont
-(UIFont *)font
{
return internalTextView.font;
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -467,13 +457,13 @@ -(UIColor*)textColor{

-(void)setBackgroundColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
[super setBackgroundColor:backgroundColor];
internalTextView.backgroundColor = backgroundColor;
}

-(UIColor*)backgroundColor
{
return internalTextView.backgroundColor;
return internalTextView.backgroundColor;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -552,12 +542,12 @@ - (UIKeyboardType)keyboardType

- (void)setEnablesReturnKeyAutomatically:(BOOL)enablesReturnKeyAutomatically
{
internalTextView.enablesReturnKeyAutomatically = enablesReturnKeyAutomatically;
internalTextView.enablesReturnKeyAutomatically = enablesReturnKeyAutomatically;
}

- (BOOL)enablesReturnKeyAutomatically
{
return internalTextView.enablesReturnKeyAutomatically;
return internalTextView.enablesReturnKeyAutomatically;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -620,7 +610,7 @@ - (void)textViewDidBeginEditing:(UITextView *)textView {


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)textViewDidEndEditing:(UITextView *)textView {
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([delegate respondsToSelector:@selector(growingTextViewDidEndEditing:)]) {
[delegate growingTextViewDidEndEditing:self];
}
Expand All @@ -635,8 +625,8 @@ - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
if(![textView hasText] && [atext isEqualToString:@""]) return NO;

//Added by bretdabaker: sometimes we want to handle this ourselves
if ([delegate respondsToSelector:@selector(growingTextView:shouldChangeTextInRange:replacementText:)])
return [delegate growingTextView:self shouldChangeTextInRange:range replacementText:atext];
if ([delegate respondsToSelector:@selector(growingTextView:shouldChangeTextInRange:replacementText:)])
return [delegate growingTextView:self shouldChangeTextInRange:range replacementText:atext];

if ([atext isEqualToString:@"\n"]) {
if ([delegate respondsToSelector:@selector(growingTextViewShouldReturn:)]) {
Expand Down
6 changes: 3 additions & 3 deletions class/HPTextViewInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ -(void)setContentOffset:(CGPoint)s
float bottomOffset = (self.contentSize.height - self.frame.size.height + self.contentInset.bottom);
if(s.y < bottomOffset && self.scrollEnabled){
UIEdgeInsets insets = self.contentInset;
insets.bottom = 8;
insets.bottom = 0;
insets.top = 0;
self.contentInset = insets;
}
}

// Fix "overscrolling" bug

if (s.y > self.contentSize.height - self.frame.size.height && !self.decelerating && !self.tracking && !self.dragging)
s = CGPointMake(s.x, self.contentSize.height - self.frame.size.height);

Expand Down Expand Up @@ -111,7 +112,7 @@ - (void)drawRect:(CGRect)rect
}
else {
[self.placeholderColor set];
[self.placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withFont:self.font];
[self.placeholder drawInRect:CGRectMake(8.0f, 8.0f, self.frame.size.width - 16.0f, self.frame.size.height - 16.0f) withAttributes:@{NSFontAttributeName:self.font}];
}
}
}
Expand All @@ -122,5 +123,4 @@ -(void)setPlaceholder:(NSString *)placeholder

[self setNeedsDisplay];
}

@end
41 changes: 0 additions & 41 deletions example/Classes/GrowingTextViewExampleAppDelegate.h

This file was deleted.

Loading