iphone常用控件之UITextView
//-(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; }