//将文本视图附加到一个已有的试图对象上 -(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;