Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
UITextView Styling and Customization
#import <QuartzCore/QuartzCore.h>
@interface <UITextViewDelegate>
#pragma mark
- (void)viewDidLoad{
[super viewDidLoad];
CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor = [UIColor lightGrayColor];
textView.delegate = self;
//CALayer, remember to import <QuartzCore/QuartzCore.h>
[[textView layer] setBorderColor:[[UIColor grayColor] CGColor]]; //border color
[[textView layer] setBackgroundColor:[[UIColor whiteColor] CGColor]]; //background color
[[textView layer] setBorderWidth:1.5]; // border width
[[textView layer] setCornerRadius:5]; // radius of rounded corners
[textView setClipsToBounds: YES]; //clip text within the bounds
[self.view addSubview:textView];
#pragma mark - UITextView
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSCharacterSet *doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet];
NSRange replacementTextRange = [text rangeOfCharacterFromSet:doneButtonCharacterSet];
NSUInteger location = replacementTextRange.location;
if (textView.text.length + text.length > 140){
if (location != NSNotFound){
[textView resignFirstResponder];
return NO;
else if (location != NSNotFound){
[textView resignFirstResponder];
return NO;
return YES;
- (void)textViewDidChange:(UITextView *)textView{
//called when texts are selected or the selection is changed, such as when copying or pasting a section of text
- (void)textViewDidChangeSelection:(UITextView *)textView{
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
- (void)textViewDidBeginEditing:(UITextView *)textView {
textView.backgroundColor = [UIColor greenColor];
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
textView.backgroundColor = [UIColor grayColor];
return YES;
- (void)textViewDidEndEditing:(UITextView *)textView{
#pragma mark
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.