Skip to content

Commit

Permalink
Created apple style Done button plus contentInset property.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspinckaers committed Aug 7, 2011
1 parent e986035 commit b98d640
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 34 deletions.
4 changes: 3 additions & 1 deletion class/HPGrowingTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
BOOL editable;
UIDataDetectorTypes dataDetectorTypes;
UIReturnKeyType returnKeyType;

UIEdgeInsets contentInset;
}

//real class properties
Expand All @@ -90,7 +92,7 @@
@property(nonatomic,getter=isEditable) BOOL editable;
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_3_0);
@property (nonatomic) UIReturnKeyType returnKeyType;

@property (assign) UIEdgeInsets contentInset;

//uitextview methods
//need others? use .internalTextView
Expand Down
37 changes: 33 additions & 4 deletions class/HPGrowingTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ - (id)initWithFrame:(CGRect)frame {
CGRect r = frame;
r.origin.y = 0;
r.origin.x = 0;

internalTextView = [[HPTextViewInternal alloc] initWithFrame:r];
internalTextView.delegate = self;
internalTextView.scrollEnabled = NO;
Expand Down Expand Up @@ -90,12 +89,34 @@ -(void)setFrame:(CGRect)aframe
{
CGRect r = aframe;
r.origin.y = 0;
r.origin.x = 0;
r.origin.x = contentInset.left;
r.size.width -= contentInset.left + contentInset.right;

internalTextView.frame = r;

[super setFrame:aframe];
}

-(void)setContentInset:(UIEdgeInsets)inset
{
contentInset = inset;

CGRect r = self.frame;
r.origin.y = inset.top - inset.bottom;
r.origin.x = inset.left;
r.size.width -= inset.left + inset.right;

internalTextView.frame = r;

[self setMaxNumberOfLines:maxNumberOfLines];
[self setMaxNumberOfLines:minNumberOfLines];
}

-(UIEdgeInsets)contentInset
{
return contentInset;
}

-(void)setMaxNumberOfLines:(int)n
{
// Use internalTextView for height calculations, thanks to Gwynne <http://blog.darkrainfall.org/>
Expand Down Expand Up @@ -190,8 +211,11 @@ - (void)textViewDidChange:(UITextView *)textView
internalTextViewFrame.size.height = newSizeH; // + padding
self.frame = internalTextViewFrame;

internalTextViewFrame.origin.y = 0;
internalTextViewFrame.origin.x = 0;
internalTextViewFrame.origin.y = contentInset.top - contentInset.bottom;
internalTextViewFrame.origin.x = contentInset.left;
internalTextViewFrame.size.height = newSizeH;
internalTextViewFrame.size.width = internalTextView.contentSize.width;

internalTextView.frame = internalTextViewFrame;

// [fixed] The growingTextView:didChangeHeight: delegate method was not called at all when not animating height changes.
Expand Down Expand Up @@ -236,6 +260,11 @@ -(void)growDidStop

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[internalTextView becomeFirstResponder];
}

- (BOOL)becomeFirstResponder
{
[super becomeFirstResponder];
Expand Down
5 changes: 2 additions & 3 deletions class/HPTextViewInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ -(void)setContentOffset:(CGPoint)s
UIEdgeInsets insets = self.contentInset;
insets.bottom = 8;
insets.top = 0;
self.contentInset = insets;
self.contentInset = insets;
}

}

[super setContentOffset:s];
}

Expand Down
53 changes: 27 additions & 26 deletions example/Classes/GrowingTextViewExampleViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,57 +55,58 @@ - (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor colorWithRed:219.0f/255.0f green:226.0f/255.0f blue:237.0f/255.0f alpha:1];

containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
containerView.backgroundColor = [UIColor whiteColor];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, 320, 40)];

textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(6, 3, 240, 40)];
textView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);

textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(6, 3, 306, 100)];
textView.minNumberOfLines = 1;
textView.maxNumberOfLines = 6;
textView.returnKeyType = UIReturnKeyGo; //just as an example
textView.font = [UIFont systemFontOfSize:15.0f];
textView.delegate = self;

textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
textView.backgroundColor = [UIColor whiteColor];

// textView.text = @"test\n\ntest";
// textView.animateHeightChange = NO; //turns off animation

textView.internalTextView.contentInset = UIEdgeInsetsMake(0, 10, 0, 0);
CGSize s = textView.internalTextView.contentSize;
s.width -= 5; // compensate for contentInset, otherwise you get vertical scrolling
textView.internalTextView.contentSize = s;

[self.view addSubview:containerView];

[textView sizeToFit];

[self.view addSubview:containerView];

UIImage *rawEntryBackground = [UIImage imageNamed:@"MessageEntryInputField.png"];
UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *entryImageView = [[[UIImageView alloc] initWithImage:entryBackground] autorelease];
entryImageView.frame = CGRectMake(5, 0, 315, 100);

entryImageView.frame = CGRectMake(5, 0, 248, 40);
entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

UIImage *rawBackground = [UIImage imageNamed:@"MessageEntryBackground.png"];
UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:background] autorelease];
imageView.frame = CGRectMake(0, 0, 315, 100);

imageView.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

// view hierachy
[containerView addSubview:imageView];
[containerView addSubview:textView];
[containerView addSubview:entryImageView];

UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
doneBtn.frame = CGRectMake(30, 30, 260, 80);
UIImage *sendBtnBackground = [[UIImage imageNamed:@"MessageEntrySendButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIImage *selectedSendBtnBackground = [[UIImage imageNamed:@"MessageEntrySendButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];

UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
doneBtn.frame = CGRectMake(containerView.frame.size.width - 69, 8, 63, 27);
doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[doneBtn setTitle:@"Done" forState:UIControlStateNormal];

[doneBtn setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
doneBtn.titleLabel.shadowOffset = CGSizeMake (0.0, -1.0);
doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0f];

[doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[doneBtn addTarget:self action:@selector(resignTextView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:doneBtn];

CGRect r = containerView.frame;
r.size.height = 40;
r.origin.y = self.view.frame.size.height - r.size.height;
containerView.frame = r;
[doneBtn setBackgroundImage:sendBtnBackground forState:UIControlStateNormal];
[doneBtn setBackgroundImage:selectedSendBtnBackground forState:UIControlStateSelected];
[containerView addSubview:doneBtn];
}

-(void)resignTextView
Expand Down
16 changes: 16 additions & 0 deletions example/GrowingTextViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
3AC80F9713EC953D00712F9A /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 3AC80F9313EC953D00712F9A /* [email protected] */; };
3AC80F9813EC953D00712F9A /* MessageEntryInputField.png in Resources */ = {isa = PBXBuildFile; fileRef = 3AC80F9413EC953D00712F9A /* MessageEntryInputField.png */; };
3AC80F9913EC953D00712F9A /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 3AC80F9513EC953D00712F9A /* [email protected] */; };
3ADB373513EE98D60006E262 /* MessageEntrySendButton.png in Resources */ = {isa = PBXBuildFile; fileRef = 3ADB373113EE98D60006E262 /* MessageEntrySendButton.png */; };
3ADB373613EE98D60006E262 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 3ADB373213EE98D60006E262 /* [email protected] */; };
3ADB373713EE98D60006E262 /* MessageEntrySendButtonPressed.png in Resources */ = {isa = PBXBuildFile; fileRef = 3ADB373313EE98D60006E262 /* MessageEntrySendButtonPressed.png */; };
3ADB373813EE98D60006E262 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 3ADB373413EE98D60006E262 /* [email protected] */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -42,6 +46,10 @@
3AC80F9313EC953D00712F9A /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
3AC80F9413EC953D00712F9A /* MessageEntryInputField.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MessageEntryInputField.png; sourceTree = "<group>"; };
3AC80F9513EC953D00712F9A /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
3ADB373113EE98D60006E262 /* MessageEntrySendButton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MessageEntrySendButton.png; sourceTree = "<group>"; };
3ADB373213EE98D60006E262 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
3ADB373313EE98D60006E262 /* MessageEntrySendButtonPressed.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MessageEntrySendButtonPressed.png; sourceTree = "<group>"; };
3ADB373413EE98D60006E262 /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* GrowingTextViewExample-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GrowingTextViewExample-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -103,6 +111,10 @@
29B97317FDCFA39411CA2CEA /* Resources */ = {
isa = PBXGroup;
children = (
3ADB373113EE98D60006E262 /* MessageEntrySendButton.png */,
3ADB373213EE98D60006E262 /* [email protected] */,
3ADB373313EE98D60006E262 /* MessageEntrySendButtonPressed.png */,
3ADB373413EE98D60006E262 /* [email protected] */,
3AC80F9213EC953D00712F9A /* MessageEntryBackground.png */,
3AC80F9313EC953D00712F9A /* [email protected] */,
3AC80F9413EC953D00712F9A /* MessageEntryInputField.png */,
Expand Down Expand Up @@ -189,6 +201,10 @@
3AC80F9713EC953D00712F9A /* [email protected] in Resources */,
3AC80F9813EC953D00712F9A /* MessageEntryInputField.png in Resources */,
3AC80F9913EC953D00712F9A /* [email protected] in Resources */,
3ADB373513EE98D60006E262 /* MessageEntrySendButton.png in Resources */,
3ADB373613EE98D60006E262 /* [email protected] in Resources */,
3ADB373713EE98D60006E262 /* MessageEntrySendButtonPressed.png in Resources */,
3ADB373813EE98D60006E262 /* [email protected] in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file added example/MessageEntrySendButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/MessageEntrySendButtonPressed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b98d640

Please sign in to comment.