UI控件(UITextView)

时间:2023-03-10 04:10:46
UI控件(UITextView)
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad]; //UITextView与UITextField主要区别:
//1、UITextView支持多行而UITextField只能是单行;
//2、UITextView继承UIScrollView,而后者继承至UIController UITextView* textView = [[UITextView alloc] init];
//注意:bounds的x、y起点都是0
textView.frame = self.view.bounds; //实现协议UITextViewDelegate
textView.delegate = self; //autoresizingMask是UIView就有的一个属性,用以调整子视图与父视图的宽高
// enum {
// UIViewAutoresizingNone = 0,
// UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
// UIViewAutoresizingFlexibleWidth = 1 << 1,
// UIViewAutoresizingFlexibleRightMargin = 1 << 2,
// UIViewAutoresizingFlexibleTopMargin = 1 << 3,
// UIViewAutoresizingFlexibleHeight = 1 << 4,
// UIViewAutoresizingFlexibleBottomMargin = 1 << 5
// };
// UIViewAutoresizingNone就是不自动调整。
// UIViewAutoresizingFlexibleLeftMargin 自动调整与父视图左边的距离,保证与父视图右边的距离不变。
// UIViewAutoresizingFlexibleRightMargin 自动调整与父视图的右边距离,保证与父视图左边的距离不变。
// UIViewAutoresizingFlexibleTopMargin 自动调整与父视图顶部的距离,保证与父视图底部的距离不变。
// UIViewAutoresizingFlexibleBottomMargin 自动调整与父视图底部的距离,保证与与父视图顶部的距离不变。
// UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与父视图左边和右边的距离不变。
// UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与父视图顶部和底部的距离不变。 //本例子为自适应高宽
textView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//是否可以编辑
textView.editable = YES; textView.backgroundColor = [UIColor whiteColor];
textView.textColor = [UIColor blueColor];
textView.font = [UIFont fontWithName:@"Arial" size:18.0];
textView.text = @"\n第1行\n第2行\n第3行\n"; [self.view addSubview:textView]; } #pragma mark - UITextView Delegate Methods
//文字改变时
- (void)textViewDidChange:(UITextView *)textView {
NSLog(@"textViewDidChange:%@", textView.text);
} //此时回车将作为提交
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"shouldChangeTextInRange:%@",text); if ([text isEqualToString:@"\n"]) {
//第一响应对象是窗口中,应用程序认为最适合处理事件的对象
//当文本框放弃第一响应对象,则软键盘退出
[textView resignFirstResponder];
return NO;
}
return YES;
} @end