//-(void)loadView{ // [super loadView]; // CGRect bounds = [[UIScreen mainScreen] applicationFrame]; // UITextView *textView = [[UITextView alloc] initWithFrame:bounds]; // self.view = textView; //} //將文本視圖附加到一個已有的試圖對象上 -(void)loadView{ [super loadView]; CGRect viewRect = CGRectMake(0, 100, 320, 200); UITextView *textView = [[UITextView alloc] initWithFrame:viewRect]; //顏色 UIColor *myColorHue = [UIColorcolorWithHue:120.0/360.0saturation:0.75brightness:0.50alpha:1.0]; // UIColor *myColorRGB = [UIColor colorWithRed:0.75 green:1.0 blue:0.75 alpha:1.0]; // UIColor *myWhiteTransparentColor = [UIColor colorWithWhite:1.0 alpha:0.50]; // UIColor *myColor = [UIColor redColor]; textView.textColor = myColorHue; //字體與大小 UIFont *myFixed = [UIFont fontWithName:@"Courier New" size:10.0]; // UIFont *mySystemFont = [UIFont systemFontOfSize:12.0]; // UIFont *myBoldSystemFont = [UIFont boldSystemFontOfSize:12.0]; // UIFont *myItalicSystemFont = [UIFont italicSystemFontOfSize:12.0]; textView.font = myFixed; textView.editable = NO; //設置為不可編輯,默認的為可編輯 //賦予內容 textView.text = @"hello .bit hold "; //分別為左中右對齊 textView.textAlignment = UITextAlignmentLeft; // textView.textAlignment = UITextAlignmentCenter; // textView.textAlignment = UITextAlignmentRight; //直接賦值 int nBottles = 100; NSString *myFormattedString = [[NSString alloc]initWithFormat:@"%d bottles of beer on the wall",nBottles]; textView.text = myFormattedString; //讀取文件 // NSString *myFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Document/file.txt"]; // NSString *myFileString = [NSString stringWithContentsOfFile:myFile encoding:nil error:nil]; // textView.text = myFileString; //鍵盤屬性 textView.keyboardType = UIKeyboardTypePhonePad; //鍵盤風格,8種 textView.keyboardAppearance = UIKeyboardAppearanceAlert; //鍵盤外觀,兩種:淺灰色或深灰色 textView.returnKeyType = UIReturnKeyGo; //回車鍵的風格 11種 textView.autocapitalizationType = UITextAutocapitalizationTypeNone;//自動大寫功能 4種 textView.autocorrectionType = UITextAutocorrectionTypeDefault; //自動更正 textView.secureTextEntry = YES; //安全文本輸入,即輸入后會顯示*號 [self.view addSubview:textView]; }
UITextView協議:
//點擊開始編輯:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
//結束編輯
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
//更改內容
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
//每次用戶通過鍵盤輸入字符時,在字符顯示在text view之前,textView:shouldChangeCharactersInRange:replacementString方法會被調用。這個方法中可以方便的定位測試用戶輸入的字符,並且限制用戶輸入特定的字符。在上面的代碼中,我使用done鍵來隱藏鍵盤:通過檢測看replacement文本中是否包含newLineCharacterSet任意的字符。
- (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; }
限制textview輸入字數:
#pragma mark - UITextViewDelegate - (void)textViewDidChange:(UITextView*)textView{ if(self.commentTextView.text.length>140){ self.commentTextView.text= [self.commentTextView.textsubstringToIndex:140]; } self.limitLabel.text= [NSStringstringWithFormat:@"%d",140- [self.commentTextView.textlength]]; //TODO:根據高度調整 if(SCREENT_HEIGHT==480) { CGSizesize = [self.commentTextView.textsizeWithFont:self.commentTextView.fontconstrainedToSize:CGSizeMake(320,440)lineBreakMode:NSLineBreakByWordWrapping]; if(size.height<=54) {//三行之內 [self.contentScrollsetContentOffset:CGPointMake(0,0)]; }else{ if(size.height-54-self.contentScroll.contentOffset.y>0) { [self.contentScrollsetContentOffset:CGPointMake(0,self.contentScroll.contentOffset.y+18)]; }else{ if(size.height-54-self.contentScroll.contentOffset.y< 0) { [self.contentScrollsetContentOffset:CGPointMake(0,self.contentScroll.contentOffset.y-18)]; } } } } } -(BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{ if(1== range.length) { returnYES; } if([textisEqualToString:@"\n"]) {//按下return鍵 [textViewresignFirstResponder]; [self.contentScrollsetContentOffset:CGPointMake(0,0)]; returnNO; }else{ if([textView.textlength] <=140) {//判斷字符個數 returnYES; } } returnNO; }