Skip to content

Instantly share code, notes, and snippets.

@vseow
Created August 7, 2015 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vseow/06923224d5e1eea88026 to your computer and use it in GitHub Desktop.
Save vseow/06923224d5e1eea88026 to your computer and use it in GitHub Desktop.
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{
NSLog(@"textViewDidChange:");
}
//called when texts are selected or the selection is changed, such as when copying or pasting a section of text
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"textViewDidChangeSelection:");
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(@"textViewShouldBeginEditing:");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"textViewDidBeginEditing:");
textView.backgroundColor = [UIColor greenColor];
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(@"textViewShouldEndEditing:");
textView.backgroundColor = [UIColor grayColor];
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"textViewDidEndEditing:");
}
#pragma mark
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[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