有时UI要开发的资源与实际frame不符。这一次,我们要绘制图片
UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0]; //stretchableImageWithLeftCapWidth使图片有拉伸效果
UITextField的属性介绍:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(80, 10, 170, 30)]; _textField = field; _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;//自己主动调整自己的位置,使自己的左边距和右边距和superview保持不变 _textField.enablesReturnKeyAutomatically = YES; //使return在输入时能够使用 _textField.placeholder = @"please input:"; //设置hint的值 _textField.textAlignment = NSTextAlignmentLeft; //文字靠左显示 _textField.borderStyle = UITextBorderStyleNone; //没有边框 _textField.font = [UIFont systemFontOfSize:18.0f]; //设置字体大小 _textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //这两个使文字会居中显示 _textField.clearButtonMode = UITextFieldViewModeWhileEditing; //当输入时有clear button UIImage* image = [[UIImage imageNamed:@"text_field_bg.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0]; //stretchableImageWithLeftCapWidth使图片有拉伸效果 20表示图片的左边的边框20像素处能够横向拉伸 _textField.delegate = self; //给textField设置代理 _textField.background = image; //背景设置 _textField.leftViewMode = UITextFieldViewModeAlways; CGRect frame = [_textField frame]; frame.size.width = 15; UIView* view1 = [[UIView alloc] initWithFrame:frame]; _textField.leftView = view1; //上面几句话设置文字跟textField的左边有些距离 [self.view addSubview:_textField];
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if ([string length]) { unichar single = [string characterAtIndex:0]; if (single > 'a' && single < 'z') { return YES; }else{//输入的数据格式不对 NSLog(@"格式不对"); [textField.text stringByReplacingCharactersInRange:range withString:@""]; return NO; } } return YES; }
假设想使键盘在进入界面的时候就弹出来,能够这样来做:
[_textFieldbecomeFirstResponder];
UIButton的属性介绍:
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; //init一个button,能够自己定义背景 [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg@2x.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateNormal]; button.frame = CGRectMake(260,10, 49, 30); //设置button的背景,有normal和highlight两种状态 [button setBackgroundImage:[[UIImage imageNamed:@"cancel_but_bg2@2x.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateHighlighted]; button.frame = CGRectMake(260,10, 49, 30); [button setTitle:@"确定" forState:UIControlStateNormal]; //设置Button的text [button setTitleColor:[MyViewController RGBColorFromHexString:@"#333333" alpha:1.0f] forState:UIControlStateNormal]; [button setTitleColor:[MyViewController RGBColorFromHexString:@"#000000" alpha:1.0f] forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside]; //假设onclick带冒号则写方法的时候会把button也传过去 像onclick:(id)sender [self.view addSubview:button];
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 46, 320, 480-46)]; _webView = webView; webView.delegate = self; NSURL* url = [NSURL URLWithString:@"http://m.baidu.com"]; NSURLRequest* request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; [self.view addSubview:webView];
代码能够在http://download.csdn.net/detail/baidu_nod/7595369下载
截图:
版权声明:本文博主原创文章,博客,未经同意不得转载。