码迷,mamicode.com
首页 > 移动开发 > 详细

IOS的UITextField,UIButton,UIWebView的一些属性介绍和IOS图片资源的使用技巧

时间:2014-07-05 22:51:09      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:uitextfield   uibutton   uiwebview   color的技巧   

有时候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];


如何设置让UITextField只能输入英文,先让Controller实现UITextFieldDelegate的代理

-(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的属性介绍:

  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下载

截图:

bubuko.com,布布扣


IOS的UITextField,UIButton,UIWebView的一些属性介绍和IOS图片资源的使用技巧,布布扣,bubuko.com

IOS的UITextField,UIButton,UIWebView的一些属性介绍和IOS图片资源的使用技巧

标签:uitextfield   uibutton   uiwebview   color的技巧   

原文地址:http://blog.csdn.net/baidu_nod/article/details/37032287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!