Giter Club home page Giter Club logo

growingtextview's Introduction

HPGrowingTextView

Multi-line/Autoresizing UITextView similar as in the SMS-app. The example project mimicks the look of Apple's SMS-app.

Screenshot

Properties

int maxNumberOfLines; // Stops growing at this amount of lines.
int minNumberOfLines; // Starts growing at this amount of lines.
int maxHeight; // Specify the maximum height in points instead of lines.
int minHeight; // Specify the minimum height in points instead of lines.
BOOL animateHeightChange; // Animate the growing
NSTimeInterval animationDuration; // Adjust the duration of the growth animation.

UITextView borrowed properties

NSString *text;
UIFont *font;
UIColor *textColor;
NSTextAlignment textAlignment;
NSRange selectedRange;
BOOL editable;
UIDataDetectorTypes dataDetectorTypes;
UIReturnKeyType returnKeyType;

If you want to set other UITextView properties, use .internalTextView

Delegate methods

-(BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView;
-(BOOL)growingTextViewShouldEndEditing:(HPGrowingTextView *)growingTextView;

-(void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView;
-(void)growingTextViewDidEndEditing:(HPGrowingTextView *)growingTextView;

-(BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
-(void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView;

// Called WITHIN animation block!
-(void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height;

// Called after animation
-(void)growingTextView:(HPGrowingTextView *)growingTextView didChangeHeight:(float)height;

-(void)growingTextViewDidChangeSelection:(HPGrowingTextView *)growingTextView;
-(BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView;

For more info, see blogpost: http://www.hanspinckaers.com/multi-line-uitextview-similar-to-sms

growingtextview's People

Contributors

agnat avatar alexeys avatar allewun avatar bretdabaker avatar d-ross avatar danpe avatar dav avatar delackner avatar dimavartanian avatar egoldfarb avatar euanlau avatar hanishabsigh avatar hanspinckaers avatar jagreenwood avatar johnpaulmanoza avatar mattjgalloway avatar mattrubin avatar palringosteve avatar ramgreenberg avatar srandazzo avatar timmehmainframe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

growingtextview's Issues

copy, paste, etc cannot be overridden by subclassing

When using a standard UITextView it is possible to override the built-in copy/paste functions with your own functionality. This is useful for more advanced copy/paste scenarios (we have the need to paste images into a UITextView.) This kind of thing is currently not possible with HPGrowingTextView because that would require overriding the functionality in HPTextViewInternal.

Cut/Paste affecting UIBarButtonItem

There seems to be a problem when you cut all text from the GrowingTextView or paste large amounts of text into an empty GrowingTextView. The images can explain it a little better:

When Pasting into empty TextView
Pasting Image
As you can see, the UIBarButtonItem gets affected in a weird way.

When Cutting or Clearing the field to empty
Cutting
Same deal here, the UIBarButtonItem gets pushed below the Toolbar's view.

**When you enter text normally, paste into a non empty textView, or if not all characters are cut/cleared
How it should be

Pasting text bug

When pasting text, HPGrowingTextView grows, however, the textView is unscrollable, and sometimes the text is cut off.

I did find that after dismissing the keyboard and having HPGrowingTextView become the firstResponder again fixes this issue, but I hope there's a solution that doesn't require this.

Xamarin iOS version

Are you aware of any Xamarin iOS version of this component (C#)? Or is it available as a precompiled library?

Invisible text on ipad?

Hi
This might be a general UITextView issue on iPad, so Im not sure if this question is targeted correctly.

Anyways, the problem I came across is that the text entered in the textview on ipad is invisible, although the same code works fine on iphone, i.e. black text is visible on white background in the textview. The project is a iphone-only app, so there are no special handling done for iPad as platform.

Below is a listing of how I create the HPGrowingTextView in viewDidLoad method, anyone having an idea how to solve the issue with invisible text on ipad?

self.chatTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(85, 5, 165, 28)];
self.chatTextView.animateHeightChange = YES;
self.chatTextView.contentInset = UIEdgeInsetsZero;
self.chatTextView.minNumberOfLines = 1;
self.chatTextView.maxNumberOfLines = kMessageChatTextViewMaxLines;
self.chatTextView.font = self.customFont;
self.chatTextView.textColor = [UIColor blackColor];
self.chatTextView.delegate = self;
self.chatTextView.backgroundColor = [UIColor whiteColor];
self.chatTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.chatTextView.layer.masksToBounds = YES;
self.chatTextView.layer.cornerRadius = 3.0;

UIImage *rawEntryBackground = [UIImage imageNamed:@"MessageEntryInputField.png"];
UIEdgeInsets imgInsets = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f);
UIImage *entryBackground = [rawEntryBackground resizableImageWithCapInsets:imgInsets];

UIImageView *entryImageView = [[UIImageView alloc] initWithImage:entryBackground];
entryImageView.frame = CGRectMake(85, 5, 165, 34);
entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

UIImage *background = [UIImage imageNamed:@"MessageEntryBackground.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
imageView.frame = CGRectMake(0, 0, self.chatViewContainer.frame.size.width, self.chatViewContainer.frame.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

UIImageView *chatboxFix = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chatbox_fix.png"]];
chatboxFix.frame = CGRectMake(85, 34, 165, 15);
[chatboxFix setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];

[self.chatViewContainer addSubview:imageView];
[self.chatViewContainer addSubview:self.chatTextView];
[self.chatViewContainer addSubview:entryImageView];
[self.chatViewContainer addSubview:self.sendButton];
[self.chatViewContainer addSubview:self.attachmentButton];
[self.chatViewContainer addSubview:chatboxFix];

Crash during growDidStop (in the latest version too)

I had this crash (reported by Crashlytics) for a while and hoped to fix it with the update to latest version of the controller. Done it few days ago, still happens.

  • (void)growDidStop
    {
    if ([delegate respondsToSelector:@selector(growingTextView:didChangeHeight:)]) {
    [delegate growingTextView:self didChangeHeight:self.frame.size.height];
    }
    }

On the first line of code (if [delegate, etc).
Exception Type: EXC_BAD_ACCESS Code: KERN_INVALID_ADDRESS at 0x3520708478
Note: I don't have this selector in my delegate. This bug occured few times for few hundred users.
Could it be that delegate is nil? Do I need to manually set the delegate?

Grow text downwards?

The GrowingTextView I want to use will be fixed to the top of the screen.

...so is there a way to grow the text downwards as they type?

iOS 7 measureHeight measures only attributedText

This line should only fire if an attributedText is really set:

if(internalTextView.attributedText && internalTextView.attributedText.length > 0){

Instead it is also true if some text is through the "text"-property, because as it is documented by Apple for UITextView:

"@Property(nonatomic, copy) NSString *text
Discussion
In iOS 6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes. Instead the text view styles the new string using the font, textColor, and other style-related properties of the class."

So what happens is that the text gets measured without any attributes. The result is (in my case) that the view never grows.

Can't change placeholder after being set

If you use something like

-(void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView {
    growingTextView.placeholder = nil;
}

On your controller, it wont work (Maybe just in iOS 7) if placeholder has been set in viewDidLoad.

Textview scrolling and scroll position problem in iOS 7

When i return NO in shouldChangeTextInRange and textview reached to its maximum height than the scrolling of textview become disabled and it scrolls to top and get stucked. When i NSLog the textView.internalTextView.isScrollEnabled it returns YES . This is happening only in iOS 7. Rest of the things working perfectly.
ios simulator screen shot jan 5 2014 11 08 50 am

"Paste" not functioning correctly

Hi,
on iOS 7, this scenario is not working correctly:

  1. setting maxLines 3 for example
  2. entering 5 line text
  3. selecting all text and choosing "Cut"
  4. paste the clipboard text back in the textView

Desired outcome:
textview functions as it should be - same as if text was entered normally

Real outcome:

  • textview frame changes to wrong position
  • scroll is disabled

Added a screenshot from example project:
screen shot
(https://f.cloud.github.com/assets/2150379/1276435/30a7f3ea-2e68-11e3-83c2-87967203106e.png)

[iOS6]HPTextViewInternal drawInRect:withattributes: crash

crash info: -[__NSDictionaryI font]: unrecognized selector sent to instance 0x14806750
2013-12-18 10:58:22.958 iHome[3060:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI font]: unrecognized selector sent to instance 0x14806750'
*** First throw call stack:
(0x21b7012 0x1fdce7e 0x22424bd 0x21a6bbc 0x21a694e 0x15575ad 0x15587c7 0x1ea24b 0x157b4be 0xd13a3f 0xd1396b 0xc25697 0xd1383c 0xd139ba 0xd132b6 0xd13994 0xd080e2 0xd0815c 0xc860bc 0xc87227 0xc878e2 0x217fafe 0x217fa3d 0x215d7c2 0x215cf44 0x215ce1b 0x2b067e3 0x2b06668 0x152b65c 0x1b265d 0x27e5)
libc++abi.dylib: terminate called throwing an exception

Fudge factor should be calculated

As the variable name fudgeFactor implies, you've worked these values out yourself and used them to reduce the overall text bounding rect within the text view's frame.

These values can actually be calculated deterministically - on iOS7 at least - the 10px width is actually the textView.textContainer.lineFragmentPadding x2 (5px default) and the 16px height is the top and bottom text container insets (textView.textContainerInset).

Repo podspec not used

The podspec in the CocoaPods spec repo does not match the one in this repo. Further, without a tag parameter, I don't believe this one would pass lint.

Because of this, CocoaPods version of the control is horribly out of date.

A new version of the podspec should be uploaded that points to the latest stable release using a git tag.

Pod::Spec.new do |s|
  s.name         = "HPGrowingTextView"
  s.version      = "0.0.1"
  s.summary      = "Multi-line/Autoresizing UITextView similar to SMS-app."
  s.description  = "An UITextView which grows/shrinks with the text and starts scrolling when the content reaches a certain number of lines."
  s.homepage     = "https://github.com/HansPinckaers/GrowingTextView"
  s.license      = { :type => 'MIT', :file => 'LICENSE.txt' }
  s.author       = { "Hans Pinckaers" => "[email protected]" }
  s.source       = { :git => "https://github.com/HansPinckaers/GrowingTextView.git", :commit => "43a7f97cfadf20fc427715a3a8e18da443c4d534" }
  s.platform     = :ios
  s.source_files = 'Classes', 'class/**/*.{h,m}'
  s.requires_arc = true
end

clipping of descenders with custom font

GrowingTextView v29-06-10

How does one configure the growingtextview to use another font?

growingtextview.font = myNewFont withSize:18pt ?

We have something similar to that, and that font will then be used (displayed) in the growingtextview. The issue we are having though is that the descenders (parts below the line in letters such as gpjqy) get clipped from time to time.

How have others worked around this issue?

Thanks in advance.

wrong height when paste

__IPHONE_7_0 used incorrectly

  • (void)drawRect:(CGRect)rect
    {
    [super drawRect:rect];
    if (self.displayPlaceHolder && self.placeholder && self.placeholderColor)
    {
    #ifdef __IPHONE_7_0
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = self.textAlignment;
    [self.placeholder drawInRect:CGRectMake(5, 8 + self.contentInset.top, self.frame.size.width-self.contentInset.left, self.frame.size.height- self.contentInset.top) withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName:self.placeholderColor, NSParagraphStyleAttributeName:paragraphStyle}];
    #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];
    #endif
    }
    }

__IPHONE_7_0 is used to determine whether or not on ios7. But this is wrong. It is only a compile time macro. So on XCode 5, it always goes through __IPHONE_7_0 path. If I use XCode to build an ios6 target, it will crash on runtime.
Here, a runtime check is needed. e.g. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

Questions

Love this feature, great for my SMS type chat APP I am currently developing, however just a few questions, my application uses Storyboard's and was wondering if you have a solution for people like me that way ? thanks :-) Totally telling my co-dev's about this

need to create tag 1.0.0 for podspec to work.

You pulled in my pull request but it currently won't work without you also setting up a release (tag) to version 1.0.0

Unfortunately I cannot do so using a pull request so you'll have to do it yourself :)

TextView doesn't grow in iOS 7

I've started debugging this to see if I can find out why but in the meantime it doesn't work at all in iOS 7. Working backwards the growingTextView delegate call never gets hit. It looks like the textview has trouble figuring out what height it should be. Have you tried running it in the iOS 7 simulator at all?

Awesome control btw, nicely done.

Can't build using the iOS 6.0 SDK

Hi,

Thanks for such a great lib, but I found a small issue when pulling today. You used the method boundingRectWithSize:options:attributes:context which is only available in 7.0 SDK, and now I can't build using 6.0.

CocoaPods Podspec Outdated

Can you update the .podspec for CocoaPods? Seems to be pointing to an outdated version without placeholder support. Thanks.

Bouncing

On iOS 4.3 bouncing doesn't work, if I remove layoutSubviews method it works.

Does not work in landscape

When switching to landscape orientation keyboardWillShow: is called again and the HPGrowingTextView flies off-screen.

Incorrect use of resolveClassMethod:

In HPGrowingTextView.m, line 292, it says:

if ([UIView resolveClassMethod:@selector(animateWithDuration:animations:)])

This is wrong, because resolveClassMethod: is used to ask the class to dynamically add a class method to the class that wasn't already in the class. However, animateWithDuration:animations: is a regular class method that is always loaded in the class. Since it's already loaded, resolveClassMethod: will return NO, and the code under it is always skipped.

In reality, what it looks like you wanted to do was check whether the class has this method. To do that, you should instead do:

if ([UIView respondsToSelector:@selector(animateWithDuration:animations:)])

(or, for the method that's actually being used in the code after,

if ([UIView respondsToSelector:@selector(animateWithDuration:delay:options:animations:completion:)])

)

Custom Font Emoji's being cut off on first line

Hi, Firstly awesome utility tanks for putting it out there for guy's like as it has really saved a heap of time.

We have slight problem, we are using a custom font which works really well with your code however if a user inputs an emoji the top of it gets cut off any ideas what we can do about this, our app is heavily styled and we really would like to keep our font

Thanks again!

Justin6

maxNumberOfLines don't work when init used instead of initWithFrame

When I initialize HPGrowingTextView object by init, max number lines restrictions don't work that is for example I set maxNumberOfLines as 4 but I can get more that 4 line while typing.
Example of initializing:

self.messageTextField = [[[HPGrowingTextView alloc] init] autorelease];
self.messageTextField.frame = CGRectMake(0, 20, 320, 40);
self.messageTextField.minNumberOfLines = 1;
self.messageTextField.maxNumberOfLines = 4;
[self.view addSubview:self.messageTextField];

Delete Method Needed to Enable Delete on a Selection

A delete method should be added so that users can delete a selected text:

-(IBAction)delete:(id)sender {
    internalTextView.text = [internalTextView.text stringByReplacingCharactersInRange:internalTextView.selectedRange withString:@""];
}

It also prevents the delete from cascading down the responder chain and removing a more root object that is attached to the view showing the current text.

in the measureHeight Function

i do not kown why you use __IPHONE_OS_VERSION_MAX_ALLOWED ? when i set baseSDK with ios7.0 in xcode5 ,and run the app int the device of ios6.0 ,it awlays go to sizeThatFits .
code:

  • (CGFloat)measureHeight
    {
    NSLog(@"%d",__IPHONE_OS_VERSION_MAX_ALLOWED);
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
    return ceilf([self.internalTextView sizeThatFits:self.internalTextView.frame.size].height);
    #else
    return self.internalTextView.contentSize.height;
    #endif
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.