Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active August 29, 2015 14:02
Show Gist options
  • Save JeOam/52bfa9d6ecbc00c81bb1 to your computer and use it in GitHub Desktop.
Save JeOam/52bfa9d6ecbc00c81bb1 to your computer and use it in GitHub Desktop.
UITextView 模板

Just like UITextField, but there are some differences listed below.

@JeOam
Copy link
Author

JeOam commented Jun 25, 2014

Placeholder in UITextView:

Easy way, just create placeholder text in UITextView by using the following UITextViewDelegate methods:

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"placeholder text here..."]) {
         textView.text = @"";
         textView.textColor = [UIColor blackColor]; //optional
    }
    [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"placeholder text here...";
        textView.textColor = [UIColor lightGrayColor]; //optional
    }
    [textView resignFirstResponder];
}

just remember to set myUITextView with the exact text on creation e.g.

UITextView *myUITextView = [[UITextView alloc] init];
myUITextView.delegate = self;
myUITextView.text = @"placeholder text here...";
myUITextView.textColor = [UIColor lightGrayColor]; //optional

and make the parent class a UITextViewDelegate before including these methods e.g.

@interface MyClass () <UITextViewDelegate>
@end

via: click

@JeOam
Copy link
Author

JeOam commented Jun 25, 2014

Style UITextview to like Rounded Rect text field:

There is no implicit style that you have to choose, it involves writing a bit of code using the QuartzCore framework:

//first, you may need...
#import <QuartzCore/QuartzCore.h>


//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];

//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor]];
[textView.layer setBorderWidth:2.0];

//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;

via clock

@JeOam
Copy link
Author

JeOam commented Jun 25, 2014

UITextView dissmissKeyBoard:

// 在输入框加一个 Done 按钮退出键盘
UIToolbar *topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];

// 这里缺少 Button1,Button2 的话,doneButton 按钮会被放到 toolbar 的左边
UIBarButtonItem *Button1 = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *Button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];

[topView setItems:@[Button1,Button2,doneButton]];

[self.textView setInputAccessoryView:topView];
-(void)dismissKeyBoard{
    [self.textView resignFirstResponder];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment