以下是三个IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用。 

UIButton按钮

  第一、UIButton的定义

  UIButton *button=[[UIButton buttonWithType:(UIButtonType);

  能够定义的button类型有以下6种,

  typedef enum {

  UIButtonTypeCustom = 0,  自定义风格

  UIButtonTypeRoundedRect,  圆角矩形

  UIButtonTypeDetailDisclosure,  蓝色小箭头按钮,主要做详细说明用

  UIButtonTypeInfoLight,  亮色感叹号

  UIButtonTypeInfoDark,  暗色感叹号

  UIButtonTypeContactAdd,  十字加号按钮

  } UIButtonType;

  第二、设置frame

  button1.frame = CGRectMake(20, 20, 280, 40);

  [button setFrame:CGRectMake(20,20,50,50)];

  第三、button背景色

  button1.backgroundColor = [UIColor clearColor];

  [button setBackgroundColor:[UIColor blueColor]];

  第四、state状态

  forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

  enum {

  UIControlStateNormal = 0, 常规状态显现

  UIControlStateHighlighted = 1 << 0, 高亮状态显现

  UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

  UIControlStateSelected = 1 << 2, 选中状态

  UIControlStateApplication = 0x00FF0000, 当应用程序标志时

  UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管

  };

  @property(nonatomic,getter=isEnabled)BOOL enabled;   // default is YES. if NO, ignores touch events and subclasses may draw differently

  @property(nonatomic,getter=isSelected)BOOL selected;  // default is NO may be used by some subclasses or by application

  @property(nonatomic,getter=isHighlighted)BOOL highlighted;

  第五 、设置button填充图片和背景图片

  [button setImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  [button setBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  第六、设置button标题和标题颜色

  [button1 setTitle: @"点击" forState:UIControlStateNormal];

  [button setTitleColor:[UIColorredColor]forState:UIControlStateNormal];

  第七、设置按钮按下会发光

  button.showsTouchWhenHighlighted=NO;

  第八、添加或删除事件处理

  [button1 addTarget:self action: @selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

  [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

  第九、 设置按钮内部图片间距和标题间距

  UIEdgeInsets insets; // 设置按钮内部图片间距

  insets.top = insets.bottom = insets.right = insets.left = 10;

  bt.contentEdgeInsets = insets;

  bt.titleEdgeInsets = insets; // 标题间距

第十、 其他

// 设置按钮为无效按钮,如果按钮无效了,按钮就不再响应用户了

btn.enabled = YES;

// 给按钮添加手势识别器

[btn addGestureRecognizer:tap];

// 添加一个按钮 ,示例

UIButton *calBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, 200, 40)];  // 按钮大小

calBtn.backgroundColor = [UIColor orangeColor];                  // 背景颜色

[calBtn setTitle:@"点我,我就计算" forState:UIControlStateNormal];            // 设置默认状态下的文字

 [calBtn setTitle:@"点我,我就计算" forState:UIControlStateHighlighted];    // 设置高亮状态下的文字   

[calBtn setBackgroundImage:[UIImage imageNamed:@"login_btn_n_Normal"] forState:UIControlStateNormal]; // 设置默认状态下的背景图片

[calBtn setBackgroundImage:[UIImage imageNamed:@"logoff_btn_n_Highlighted"] forState:UIControlStateHighlighted];   // 设置高亮状态下的背景图片

[self.view addSubview:calBtn];  // 最会一定要添加按钮

【注】图片的名称要提前修改好,最好在后面加上分辨是默认状态还是高亮状态的单词

  UILabel标签

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 160)];  // 大小

lbl.backgroundColor = [UIColor lightGrayColor]; // 背景颜色

lbl.textColor = [UIColor blueColor];     // 字体颜色

// lbl.shadowColor = [UIColor redColor];      // 阴影效果,不常用

// lbl.shadowOffset = CGSizeMake(4, -10);

lbl.text = @"宿舍的";    // 添加文字

// 标签内容对齐方式

lbl.textAlignment = NSTextAlignmentCenter;

// 设置标签的行数,如果设置为0,表示可以有任意多行

lbl.numberOfLines = 2;

// 当标签有多行时,设置换行方式 ,默认的是以单词为单位

lbl.lineBreakMode = NSLineBreakByTruncatingMiddle;  // 如果不能完全显示,中间会有三个小点

// 设置标签高亮状态

lbl.highlighted = YES;

// 设置标签高亮时字体颜色

lbl.highlightedTextColor = [UIColor purpleColor];

// 允许用户可以与标签进行交互

lbl.userInteractionEnabled = YES;       //允许用户交互

// 定义一个点击手势识别器对象

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lblClicked:)];

// 在标签上添加一个手势识别器

[lbl addGestureRecognizer:tap];

//  lbl.enabled = NO;

lbl.adjustsFontSizeToFitWidth = YES;

// lbl.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

[self.view addSubview:lbl]; // 控件最后都需要添加

【小结】下面的大家可以试着用一下,

UIButton、UILabel、UITextField 初学者需要了解的基本定义和常用设置的更多相关文章

  1. swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel // // ViewController.swift // ...

  2. iOS学习21之UILabel, UITextField, UIButton, UIImageView

    1.UILabel 1> 概述 UILabel (标签): 是显示文本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类一般是为了扩充父类 ...

  3. UILabel,UITextField,UIButton三大基础控件总结

    (一)UILabel空件 属性: 1.背景颜色 label.backgroundColor = [UIColor ***]; 2. 显示文字: label.text = @"******&q ...

  4. UILabel,UITextField 以及UIButton应用

    </pre><pre name="code" class="cpp">一.UILabel 它是ioS开发使用的控件来显示文本,它是UIV ...

  5. UI 经常用法总结之--- UILabel UITextField (不断更新中)

    UILabel : UIView <NSCoding> 1.创建一个UILabel对象 UILabel *label = [[UILabel alloc]initWithFrame:CGR ...

  6. iOS中UITextField常用设置和方法

    //初始化textField并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; // ...

  7. 初学者刚学c++在定义类时只有成员变量易犯的错误

    ------------------------ #include<iostream> using namespace std;//c++的命名空间 class circle { publ ...

  8. Java初学者作业——完成对已定义类(Admin)的对象的创建。并完成属性的赋值和方法的调用。

    返回本章节 返回作业目录 需求说明: 完成对已定义类(Admin)的对象的创建.并完成属性的赋值和方法的调用. 实现思路: 创建 MyTest 类,并添加 main函数. 在 main函数中完成对 A ...

  9. UI入门指引

    1. iOS学习路线: C语言:数据类型.流程控制.函数.指针.字符串.结构体.枚举.预处理: OC:面向对象.内存管理.分类.协议.Block.KVC/KVO.Foundation框架: iOS基础 ...

随机推荐

  1. 一个有意思的js小问题

    问题:如何实现以下函数? add(2, 5); // 7 add(2)(5); // 7 第一个就不用说了,很简单,关键是看第二个,add(2)(5),可见add(2)应该返回的是一个函数,这个函数再 ...

  2. 激活当前视图菜单高亮呈现 V2.0

    前一段时间,Insus.NET有分享一篇<激活当前视图菜单高亮呈现>http://www.cnblogs.com/insus/p/5287093.html 这篇只是同一控制器的菜单. 今天 ...

  3. iOS学习笔记——多控制器管理

    NavigationController 在StoryBoard中添加NavigationController 在上网看到很多都是用xib添加,使用StoryBard的有两种办法,但我觉得下面用到那种 ...

  4. Model元数据定制与Model模板

    元数据这一词对于计算机科学来说不算陌生,对元数据的解释最简单的解释就是描述数据的数据,那么Model元数据当然是描述Model中各种成员的数据了,在ASP.NET MVC中ModelMetadata这 ...

  5. 【阿炬Android笔记】01、调用VitamioBundle播放窗口

    1.调用VitamioBundle播放窗口 Intent intent = new Intent(getApplicationContext(), VideoActivity.class); inte ...

  6. 【C#进阶系列】16 数组

    首先提一下,个人在项目中已经很少用到数组了,更多的时候使用List<>. 数组大小固定,如果只是用来存放数据,专门用来读取,更改当然方便.但是更多的时候我们需要进行增删改,这个时候用Lis ...

  7. Ibatis中常见错误解决方案

    在Ibatis 的sqlMap或者sqlMapConfig配置文件中如果出现以下错误信息: Referenced file contains errors (http://www.ibatis.com ...

  8. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

    场景:eclipse中编写java中用到数组 问题: 程序不报错但是运行过程中 终止,显示字样 “ Exception in thread "main" java.lang.Arr ...

  9. Web-Nginx配置

    #运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ...

  10. (转)B-树、B+树、B*树

    B-树 是一种多路搜索树(并不是二叉的): 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: 4. ...