2013년 9월 4일 수요일

How to add UITextView in COCOS2D-iPhone




COCOS2D-iPhone에서 "UITextView"를 사용하기 위해서는 일단 클래스에 <UITextViewDelegate>를 추가해주어야 한다.

1. UITextView 정의

UITextView* _textView = [[UITextView alloc]initWithFrame:CGRectMake(x, y, w, h)];
    [_textView setBackgroundColor:[UIColor clearColor]];
    [_textView setKeyboardType:UIKeyboardTypeDefault];
    [_textView setFont:[UIFont fontWithName:@"Nanum Brush Script" size:24.0f]];
    // 네 변의 padding값 설정
    [_textView setContentInset:UIEdgeInsetsMake(t, 0.0f, 30.0f, 0.0f)];
    [_textView setScrollEnabled:YES];
    [_textView setDelegate:self];

2. UITextView 추가

[[[CCDirector sharedDirector]view]addSubview:_textView];


3. UITextView 제어

// 편집 시작시점에 발생하는 이벤트
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    // textView를 키보드 위로 올라 오게끔 설정
    _textView.frame = CGRectMake(_textView.frame.origin.x,
                                 _textView.frame.origin.y, 
                                 _textView.frame.size.width,
                                 _textView.frame.size.height - 215); //resize
}
// 편집 끝나고 focus를 잃는 시점에 발생하는 이벤트
- (void)textViewDidEndEditing:(UITextView *)textView
{
    // 키보드를 숨길 것임으로 textView를 원래 사이즈로 복원
    _textView.frame = CGRectMake(_textView.frame.origin.x,
                                 _textView.frame.origin.y,
                                 _textView.frame.size.width,
                                 _textView.frame.size.height + 215);
}
// textView에 글자수가 변경될 때 발생하는 이벤트
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // 여기서 text는 현재 키보드 조작으로 입력된 텍스트를 의미한다.
}
// textView의 내용이 변경될 때 발생하는 이벤트
- (void)textViewDidChange:(UITextView *)textView
{
    // 입력된 총 글자수 확인은 이 메서드에서 하는것이 정확한 듯
}

3. UITextView의 제거

[_textView removeFromSuperView];
-----------------------------------------------------------------------------------------------------------------------
Pearl Fisher

전혀 낚시같지 않은 신개념 낚시게임이에요~


진주캐기 보러가기 클릭!