Created
December 26, 2013 05:45
-
-
Save keicoder/8130249 to your computer and use it in GitHub Desktop.
objective-c : UITextView 생성, 델리게이트 메소드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UITextView 생성, 델리게이트 메소드 | |
#import <QuartzCore/QuartzCore.h> | |
.h 파일 | |
@interface 에서 <UITextViewDelegate> 명시 | |
.m 파일 | |
#pragma mark - 뷰 라이프 사이클 | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
//코드 방식으로 UITextView 생성 | |
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 클래스 이용 텍스트 뷰 커스터마이징, 레이어 프로퍼티 사용을 위해 <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{ | |
//newLineCharacterSet이 있으면 done button이 호출됨. 따라서 키보드가 사라짐. | |
NSCharacterSet *doneButtonCharacterSet = [NSCharacterSet newlineCharacterSet]; | |
NSRange replacementTextRange = [text rangeOfCharacterFromSet:doneButtonCharacterSet]; | |
NSUInteger location = replacementTextRange.location; | |
//텍스트가 140자가 넘지 않도록 제한 | |
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