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

有时候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:@"[email protected]"] stretchableImageWithLeftCapWidth:20 topCapHeight:0] forState:UIControlStateNormal];
    button.frame = CGRectMake(260,10, 49, 30);  //设置button的背景,有normal和highlight两种状态
    [button setBackgroundImage:[[UIImage imageNamed:@"[email protected]"] 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下载

截图:

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

时间: 2024-08-05 22:25:08

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

iOSDay21之UILabel, UITextField, UIButton, UIImageView

1.UILabel 1> 概述 UILabel (标签): 是显示本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类 般是为了扩充父类的功能UILabel扩展了文字显示的功能, UILabel 是能显示文字的视图. 2> 创建UILabel的步骤 创建UILabel与创建UIView的步骤很相似. 开辟空间并初始化(如果本类有初始化方法,则使用自己的初 始化方法;否则使用父类的) 设置文本控制相关的属性 添加到父视图上,用以显示 释放所

UI基础-UITextField &#160; &#160;UIButton &#160; delegate

UITextField UITextField(输入框):是控制文本输入和显示的控件,在App中UITextField出现频率也比较高. Ios系统借助虚拟键盘实现输入,当点击输入框,系统会自动调出键盘,方便你进一步操作.在你不需要输入的时候,可以使用回收键盘的方法,收回弹出的键盘. UITextField和UILabel相比,UILabel主要用于文字显示,不能编辑,UITextField允许用户编辑文字(输入) 文本显示 外观控制 UIButton UIButton(按钮):是响应用户点击的

ios网络学习------4 UIWebView的加载本地数据的三种方式

UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIME的标识符等放入传送的数据中告诉浏览器使用那种插件读取相关文件. uiwebview加载各种本地文件(通过loadData方法): - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBund

[转]iOS中UITextField 使用全面解析

001//初始化textfield并设置位置及大小002  UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];003  004//设置边框样式,只有设置了才会显示边框样式 005  text.borderStyle = UITextBorderStyleRoundedRect;006 typedef enum {007    UITextBorderStyleNone,008   

iOS开发 - 设立UIButton的Image为Aspect Fit

Button setImage设置的图片默认是会拉伸缩放的,如果我想要Aspect Fit的效果,要如何做呢?一开始我想到了用contentMode属性,很可惜不起作用.后来我发现button有一个imageView属性,设置它的contentMode就OK了.代码如下: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.imageView.contentMode = UIViewContentMode

IOS UIView(UIButton)通过显示动画移动的时候 响应点击的解决方案

今天在做一个UIButton显示动画的时候,遇到一个问题,就是在移动的时候 ,需要相应它的点击时间(click) 通过CAKeyframeAnimation 来移动UIButton的layer ,效果可以,但是就是无法点击. 解决方法: 将UIButton 的  userinterfaceenable 设置为NO 为UIButton的父view添加tap点击事件 点击事件实现如下 -(void)viewClicked:(UITapGestureRecognizer*)gesture { CGPo

IOS开发之使用UIWebView实现图文混排

IOS开发之使用UIWebView实现图文混排时间 2014-08-15 13:45:35  CSDN博客原文  http://blog.csdn.net/wildcatlele/article/details/38583731主题 UIWebView一开始做第一个新闻资讯项目的时候,不知道可以使用UIWebView实现图文并茂的效果,于是就用了最笨的方法,使用TableView解决的新闻浏览.当有点项目经验后知道可以使用UIWebView或者CoreText实现,一直也没有尝试.当上次面试被问

ios开发中UIButton的使用(一)

ios开发中UIButton的使用(一) 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状态 normal(普通状态) 默认情况(Default) 对应的枚举常量:UIControlStateNormal highlighted(高亮状态) 按钮被按下去的时候(手指还未松开) 对应的枚举常量:UIControlStateHighlighted disabled(失效状态,不可用状态

iOS开发网络篇—UIWebView简单介绍

iOS开发网络篇—UIWebView简单介绍 一.简单介绍 1.简单说明 UIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但能加载远程的网页资源,还能加载绝?部分的常见文件 html\htm pdf.doc.ppt.txt  mp4 ...... 2.UIWebView常?的加载资源的方法 - (void)loadRequest:(NSURLRequest *)request; 3.常用的属性和方法 重新加载(刷新)