iOS开发~视图(UIView)与控件(UIControl)
1.UIView类
1.什么是视图
看得见的都是视图
2.什么是控件
一种特殊的视图,都是UIControl的子类,不仅具有一定的显示外观,还能响应高级事件,与用户交互。严格意义上UILabel不是控件,因为label不能响应用户交互事件。
3.术语的理解:
视图:一个大一点的显示区域,里面可以容纳控件,做容器讲
控件:容器中包含的子元素
2.UILabel标签
1. 是什么?
静态文本内容的展示控件
2.label属性
1)text:显示文本的内容
2)font:显示文本的字体
3)numberOfLines:默认为1,显示的最大行数,0表示无上限
4)lineBreakMode:换行模式, 省略头或尾
NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */
NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */
NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz"
5)adjustsFontSizeToWidth:是否调整字体大小适应控件宽度 YES ;
6) textColor:设置文本的颜色
例:
在 ViewController.m中
- (void)viewDidLoad
{
[super viewDidLoad];
//设计view
UILabel* label=[[UILabel alloc]init];
label.frame=CGRectMake(30, 200,400, 60);
//设置字体颜色
label.textColor=[UIColor whiteColor];
//设置最大显示行数
label.numberOfLines=2;
//设置标签内容的字体
label.font=[UIFont systemFontOfSize:20];
//设置换行模式
label.lineBreakMode=NSLineBreakByTruncatingHead;
//调整字体大小
//label.adjustsFontSizeToFitWidth=YES;
label.text=@"SunShine SunShine SunShine SunShine SunShine SunShine SunShine SunShine SunShine SunShine";
//添加到控制器自带的那个视图里面
[self.view addSubview:label];
}
@end
效果如下:

3.UIButton按钮
1.什么是按钮?
可以与用户交互,能够点击的一种控件
2.创建方式
工厂方法创建,使用系统模式
3.常用属性
1)frame :按钮大小
2)backgroundColor:按钮背景色
3)setBackgroundImage:按钮背景图
1.点击images.xcassets文件,将要添加的图片拖拉进文本框,左边框修改图片名字
2.点击下方Show Slicing按钮
3.在下方进行缩小放大操作
4.点击图片中Start Slicing按钮进行裁剪,再点击中间按钮
5.九切片:横3线:1线范围不变,1-2线之间复制,2-3线裁剪省掉,3线不变
6.将照片名字添加到程序
//[button setBackgroundImage:[UIImage imageNamed:@"2a"] forState:UIControlStateNormal];
4)tintColor:按钮字体颜色
5) setTitle:点击按钮的状态
UIControlStateNormal = 0, 正常按下
UIControlStateHighlighted = 1 << 0, 长按状态
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
4.添加事件
***点一次按钮,执行调用一次方法
addTarget:为按钮添加响应事件,即点击按钮时需实现的功能
参数: 1.target:让当前控制器对象成为处理响应的对象
2.action:处理事件的对象所使用的方法
3.events:添加对按钮的什么事件的处理
[button addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
例:
在ViewController.m中
#import "ViewController.h"
@interface ViewController ()
//设置全局变量
@property(nonatomic,strong)UILabel *label;
@end
@implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
//使用工厂方法创建button对象
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置frame属性
button.frame = CGRectMake(100,200,100, 40);
//设置按钮上的文字
[button setTitle:@"切换" forState:UIControlStateNormal];
// [button setTitle:@"KO" forState:UIControlStateHighlighted];
//设置按钮的背景色
//button.backgroundColor = [UIColor lightGrayColor];
//设置按钮的背景图
//[button setBackgroundImage:[UIImage imageNamed:@"2a"] forState:UIControlStateNormal];
//设置按钮的图片
[button setImage:[UIImage imageNamed:@"qw"] forState:UIControlStateNormal];
//为按钮添加响应事件
//target:让当前控制器对象成为处理响应的对象
//action:处理事件的对象所使用的方法
//events:添加对按钮的什么事件的处理
[button addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
//添加按钮到视图中
[self.view addSubview:button];
//添加一个UILable
UILabel *label = [[UILabel alloc]init];
self.label = label;
label.frame = CGRectMake(130,150,400, 40);
label.text = @"SunShine";
[self.view addSubview:label];
}
//处理事件的对象所使用的方法
-(void)Click{
self.label.text = @"Hello SunShine";
}
@end
效果如下:

在AppDelegate.h中
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor=[UIColor greenColor];
//1.创建控制器的实例 myVC自带一个视图
ViewController* myVC=[[ViewController alloc]init];
//2.将控制器设置为window的根视图控制器
self.window.rootViewController=myVC;
//self.window.rootViewController=[ViewController new];
[self.window makeKeyAndVisible];
return YES;
}
做一个小的应用
界面中有一个按钮,每次按下按钮,界面多一个UILabel
要求:
1)label之间间隔10个点的距离
2)所有label和屏幕左边距离20个点
3)所有label宽300,高30
4)每个Label的内容进行叠加(Hello,Hello
SunShine,HelloSunShineSunShine,........)
#import "ViewController.h"
@interface ViewController ()
//@property(nonatomic,strong)UILabel* label;
@property(nonatomic,assign)int y;
@property(nonatomic,strong)NSMutableString* str;
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
UIButton* button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(130,50,100,40);
button.backgroundColor=[UIColor whiteColor];
[button setFont:[UIFont systemFontOfSize:20]];
button.tintColor=[UIColor redColor];
[button setTitle:@"呈现" forState:UIControlStateNormal];
//[button setTitle:@"off" forState:UIControlStateHighlighted];
//响应事件
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//初始化
self.y=100;
self.str=[[NSMutableString alloc]initWithString:@"Hello"];
}
-(void)show{
UILabel* label=[[UILabel alloc]init];
//label.backgroundColor=[UIColor purpleColor];
label.text=self.str;
label.frame=CGRectMake(20,self.y,300,30);
label.adjustsFontSizeToFitWidth=YES;
label.font=[UIFont systemFontOfSize:18];
label.textColor=[UIColor redColor];
[self.view addSubview:label];
//只改变当前使用的值,出了范围就是改变后的值
self.y+=35;
[self.str appendString:@"SunShine"];
}
@end
效果如下:

iOS开发~视图(UIView)与控件(UIControl)的更多相关文章
- ios开发中关闭textview控件的虚拟键盘
在ios开发中,textfield控件在点击的时候出现虚拟键盘,关掉虚拟键盘可以通过虚拟键盘中的done button和点击view中的任意地方来关闭虚拟键盘. 1.第一种方法是textfield控件 ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
- iOS开发之七:常用控件--UISlider、UISegmentedControl、UIPageControl的使用
一.UISlider的使用 其实UISlider在iOS开发中用的似乎不是很多,我们看到的用到的地方多是音乐播放器的音量控制,以及视频播放器中的音量控制. 还是记录一下吧! 1.常用属性 // 设置获 ...
随机推荐
- oracle常用自定义函数集合
1.Oracle 判断值是否为数字的函数CREATE OR REPLACE FUNCTION ISNUMBER(MyStr VARCHAR2) RETURN NUMBERIS STR VARCHAR ...
- Objective-C 字符串
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...
- window.open() | close()方法
Window对象的open()方法可以打开一个新的浏览器窗口(或标签页),window.open()载入指定的URL到新的或已存在的窗口中,返回代表那个窗口的window对象,它有4个可选的参数 1. ...
- 使用MSSM管理工具登录LOCALDB
调试程序没有安装 sql server时,可以使用localdb.这是一个简易的sql server数据库,用于本地测试是很方便,省去安装SQL SERVER的工作 电脑上安装了VS2013 VS20 ...
- Hdu1096
#include <stdio.h> int main() { int T,n; ; while(scanf("%d",&T)!=EOF){ while(sca ...
- 【iOS开发】添加子控件方式(懒加载,GCC)
// // ViewController.m // GCC // // Created by admin on 15/10/7. // Copyright © 2015年 admin. All rig ...
- Find out C++ Memory Usage in Code
You can use Microsoft Platform SDK functions to get the heap size at a specific point. If get the he ...
- 【转】Device Tree(三):代码分析
原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...
- Android平台的事件处理机制和手指滑动例子
Android平台的事件处理机制有两种 基于回调机制的事件处理:Android平台中,每个View都有自己的处理事件的回调方法,开发人员可以通过重写View中的这些回调方法来实现需要的响应事件. 基于 ...
- Thinkphp 3.0版本上传文件加图片缩略图实例解析
先看html加个表单,注意这里的action 路径要选 对. <div> <form action="__URL__/add_img" enctype=" ...