UITextView控件詳解 – iPhone手機開發技術文章 iPhone軟體開發教學課程

self.textView = [[[UITextView  alloc] initWithFrame:self.view.frame] autorelease]; //初始化大小並自動釋放
 2
 3 self.textView.textColor = [UIColor blackColor];//設置textview裡面的字體顏色 
 4
 5 self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設置字體名字和字體大小 
 6
 7 self.textView.delegate = self;//設置它的委托方法 
 8
 9 self.textView.backgroundColor = [UIColor whiteColor];//設置它的背景顏色
10
11 self.textView.text = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";//設置它顯示的內容 
12
13 self.textView.returnKeyType = UIReturnKeyDefault;//返回鍵的類型 
14
15 self.textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型 
16
17 self.textView.scrollEnabled = YES;//是否可以拖動 
18
19 self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度
20
把鍵盤的回車鍵當初鍵盤彈回按鈕
pragma mark – UITextView Delegate Methods    

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{   

    if ([text isEqualToString:@"\n"]) {   

        [textView resignFirstResponder];   

        return NO;   

    }

    return YES;   

}
對textView字數限制
– (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text   
{   
    if (range.location>=100)    
    {   
        return  NO;   
    }   
    else    
    {   
        return YES;   
    }   
}   
作者:lmmilove

發佈留言