
用于显示文本,文本是对一些东西的说明。标签继承于UIView。
//1创建(在系统中存在alloc)
UILabellabel = [[UILabel alloc] init];
//2美化(设计位置大小背景颜色显示文本等等)
/
frame设置位置和大小。位置(x,y)大小(weight,hight)。
frame{x,y,weight,hight} 结构体struct
frame包括origin/size:origin{x,y}size{weight,hight}
frame是CGRect类型(结构体,包括CGPoint类型的origin和CGSize类型的size),origin包括CGFloat类型的x和y ;size包括CGFloat类型的size和height。
CGRectMake(<#CGFloat x#>, <#CGFloat y#>,<#CGFloat width#>, <#CGFloat height#>);设置大小和位置。
/
labelframe=CGRectMake(0, 0, 100, 100);
//设置背景颜色
labelbackgroundColor=[UIColor yellowColor];
//text属性设置标签的文本
labeltext=@"标签控件";
//textAlignment设置文本对齐方式枚举类型
//NSTextAlignmentCenter居中对齐
//NSTextAlignmentRight居右
//NSTextAlignmentLeft居左
labeltextAlignment=NSTextAlignmentCenter;
//textColor设置文本颜色
labeltextColor= [UIColor blackColor];
//font设置字体大小默认字体大小是17
labelfont=[UIFont systemFontOfSize:14];
//numberOfLines设置多行显示当设置为0时自动分行
labelnumberOfLines= 0;
//shadowOffset设置阴影
//第一个数值为正则向右偏移否则向左偏移
//第二个数值为正则向下偏移否则向上偏移
labelshadowOffset=CGSizeMake(-2, -2);
//shadowColor设置阴影颜色
labelshadowColor= [UIColor redColor];
//3添加(把控件贴到需要显示的地方)
//addSubview添加子视图这里让标签添加到窗口上
//这里selfview叫做label的父视图
[selfview addSubview:label];
用于控制行为的发生或者属性的改变
//1创建
//创建对象分配内存并初始化
UIButtonbutton=[[UIButton alloc] init];
//2美化
buttonframe=CGRectMake(100, 100, 100, 40);
//设置背景颜色
buttonbackgroundColor= [UIColor blueColor];
//设置按钮的标题不能通过属性设置
//第一个参数setTitle是按钮的标题字符串
//第二个参数forState是设定这个标题所处的状态枚举类型
//UIControlStateNormal通常状态
//UIControlStateHighLighted高亮状态
//UIControlStateDisabled不可使用的状态
//UIControlStateSelectwd选择下的状态
[buttonsetTitle:@"开始" forState:UIControlStateNormal];
[buttonsetTitle:@"关闭" forState:UIControlStateHighlighted];
//UIImage类工具类继承于NSObject
UIImageimage = [UIImage imageNamed:@"1png"];
//设置按钮(根据自身大小进行添加)
//第一个参数setImage设置的UIImage类型
//第二个参数forState是设置这个的状态枚举类型
//[button setImage:image forState:UIControlStateNormal];
//设置一般状态下的之后点击变暗设置高亮状态下的就可以消除这个bug
//[button setImage:image forState:UIControlStateHighlighted];
//设置按钮的背景(适应大小,自动拉伸)
[button setBackgroundImage:imageforState:UIControlStateNormal];
[button setBackgroundImage:imageforState:UIControlStateHighlighted];
//按钮绑定方法
//第一个参数addTarget添加目标,点击按钮之后谁去执行按钮的方法self自己执行
//第二个参数action行为方法,按钮绑定的方法
//第三个参数forControlEvents控制事件,按钮在什么条件下去触发绑定的方法枚举//UIControlEventTouchUpInside单击
//这里给按钮绑定方法括号中相当于声明了方法。但是这个方法需要在这个类中实现,否则运行点击按钮会崩溃
[button addTarget:selfaction:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
//边框宽度通过设置layer打点可以对控件进行切边,但是两个属性要同时使用才有效果
ButtonlayerborderWidth= 10;
//边框颜色
ButtonlayerborderColor= [UIColor colorWithRed:197 /2550green:197 / 2550blue:197 / 2550alpha:1]CGColor;
ButtonlayerborderColor= (__bridgeCGColorRef_Nullable)([UIColor colorWithRed:197 /2550green:197 / 2550blue:197 / 2550alpha:1]);
//3添加
//添加到窗口addSubview上添加子视图
[selfview addSubview:button];
用于输入文本内容
//1创建内存分配内存并且初始化
UITextField textfield =[[UITextField alloc] init];
//2美化
//设置位置和大小
textfieldframe=CGRectMake(100, 100, 100, 40);
//设置背景颜色
textfieldbackgroundColor= [UIColor redColor];
//borderStyle设置边框类型枚举类型
//UITextBorderStyleRoundedRect圆角矩形
//UITextBorderStyleline线性
//UITextBorderStyleBezel刃型
textfieldborderStyle=UITextBorderStyleRoundedRect;
//placeholder设置提示语
textfieldplaceholder=@"请输入密码";
//设置安全输入。布尔类型设置YES;输入内容会变为小黑点
// textfieldsecureTextEntry = YES;
//设置输入框的文本(获取输入框的文本也是这个属性)
textfieldtext=@"123";
//设置清除按钮的样式枚举样式
//UITextFieldViewModeWhileEditing当编辑时候存在(光标一定在输入框中的)//UITextFieldViewModeUnlessEditing除了编辑的时候(只要输入框中有内容)//UITextFieldViewModeAlways总是存在
textfieldclearButtonMode=UITextFieldViewModeWhileEditing;
//是否纠错,输入时会提示正确的内容
textFieldautocorrectionType=UITextAutocorrectionTypeNo;
//设置return键的样式枚举类型
textfieldreturnKeyType=UIReturnKeySend;
//设置键盘的样式
//UIKeyboardTypeNumberPad纯数字键盘
textfieldkeyboardType=UIKeyboardTypeNumberPad;
//enabled设置输入框不可使用
textfieldenabled=YES;
//设置每次开始编辑时清除原输入框的内容
textfieldclearsOnBeginEditing=YES;
//键盘下去3种方法(失去第一响应者。输入框绑定方法。touchesBegan。)
//3添加到窗口上
[selfview addSubview:textfield];
视图
//UIImageView专门用来显示的视图,继承于uiview
//1创建
UIImageView imageView = [[UIImageView alloc] initWithFrame:[[UIScreenmain Screen] bounds]];
//2美化
//设置位置和大小
//imageViewframe = CGRectMake(40, 80, 200, 300);
//设置
//创建一张用于视图的显示
UIImage image = [UIImage imageNamed:@"6png"];
//image视图的属性用于设置
imageViewimage= image;
//userInteractionEnabled 设置用户交互性
imageViewuserInteractionEnabled=YES;
//3添加
[selfview addSubview:imageView];
//接收web事件
-(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType{
if ([requestmainDocumentURLrelativePath isEqualToString:@"/alert"]) {
UIAlertView alert = [[UIAlertView alloc] initWithTitle:nil message:@"本地代码执行" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
return false; //执行本地代码,返回false不让网页读取网络资源
}
return true; //为yes加载内容,否则不
}
开发过程中经常遇到通过imageEdgeInsets和titleEdgeInsets去设置button中的image,title的位置,来达到想要的效果。但因为对其原理的不了解,经常碰壁,设置了却达不到自己想要的效果。终于找到这篇文章,文章作者解析的很详细,这里记录一下,方便自己日后查阅。
1常用的button样式,在上,文字在下
你这有问题啊。- (IBAction)createBtn:(id)sender { 里的button 不是你原来创建的那些啊。
你要获取原来创建的button然后调整其frame 和 背景色。
用viewWithTag 获取
本节学习内容:
1UIButton的控件基本概念
2UIButton的创建方法
3UIButton的类型
4可显示的UIButton
viewControllerm
-(void)createUIRectButton{
//创建一个btn对象,根据类型来创建button
//圆角类型btn:UIButtonTypeRoundedRect
//通过类方法来创建ButtonWithType:类名+方法
UIButton btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置button按钮的位置
btnframe=CGRectMacke(100,100,100,40);
//设置按钮的文字内容
// @parameter p1:字符串类型,显示到按钮上的文字,
//P2:设置文字显示的状态类型:UICountrolStateNormal,正常状态
btn setTitle:@"按钮01" forState:UICountrolStateNormal];
//P1显示文字
//P2显示状态:UICountrolStateHighLighted
btn setTitle:@"按钮按下" forState:UICountrolStateHighLighted];
//设置button背景颜色
btnbackgroundColor=[UIColor grayColor];
//设置文字颜色
//p1 颜色,p2状态
[btn setTitleColor:[UIColor redColor] forState:UIControlStaNromal];
//设置按下状态的颜色
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHiglighted];
//设置按钮风格颜色
[btn setTintColor:[UIColor whitecolor]];
//注:setTitleColor 优先级高于setTintColor
//titleLabel:UILabel空控
btn,titleLablefont=[UIFount systemFontOfSize:12];
//添加到试图中并显示
[selfview addSubview:btn]
}
//创建一个可以显示btn
-(void)createImageBtn{
//创建一个自定义类型的btn
UIButton btnImage=[Button vuttonWithType:UIButtonTypeCustom];
//Btn位置
btnImageframe=CGRectMake(100,200,100,100);
//默认
UIImage icon01=[UIImage imageNamed:@"btn02jpg"];
//点击后的
UIImage icon02=[UIImage imageNamed:@"btn03jpg"];
//设置按钮方法设置 p1:显示的对象,p1控件状态
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[selfview addSubview:btnImage];
}
UIButton事件
本节学习内容:
1UIButton的事件的概念
2UIButton的添加方法
3UIButton的响应函数
4多按钮使用同一事件函数
viewControllerh
-(void)createBtn{
//创建圆角按钮
UIButton btn=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];
btnfram=CGRectMake(100,100,80,40);
[btn setTitle:@"按钮" froState:UIControlStateNormal];
//给按钮事件函数
//参数P1:谁来实现事件函数,实现者对像都就是“谁|"
//参数P2:@selector(pressBtn)函数对像,当按钮满足P3事件类型时,庙用函数
//参数p3:UIControlEvent:事件处理函数类型
//UIControlEventTouchUpinside:当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数
//UIControlEventTouchDowninside:当手指触摸屏幕上触发
/
调用不带参数事件
btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];
/
/
调用不带参数事件
btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];
/
//带参数事件
btn addTarget:self acion:@selector(pressBtn:)forCountrolEvents:UIControlEventTouchUpinside];
//确摸时调用事件函数
[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown]
[selfveiw addSubView:Btn];
UIButton btn02=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];
btn02fram=CGRectMake(100,200,80,40);
[btn02 setTitle:@"按钮" froState:UIControlStateNormal];
//可以多个按钮使用同一个事函数来处理不同按钮事件
[btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchDown];
[selfveiw addSubView:Btn02];
//设置按钮标记值
btntag=101;
btn02tag=102;
}
-(void)pressBtn02{
if(btntag==101){
NSLog(@"btn 01 pressed ");
}
if(btntag==102){
NSLog(@"btn 02 pressed");
}
-(void)touchDown
{
NSLog(@"按钮被触摸!");
}
-(void)pressBtn{
NSLog(@"按钮被按了一下!");
}
//参数为按钮本身
-(void)pressBtn:(UIButton)btn{
NSLog(@"tbn pressed");
}
如果你用的是IB,选中按钮在右侧属性界面进行设置:按钮类型(Type)设置为自定义,BackgroundImage选择你的,Title内输入你要显示的文字代码实现的话如下://按钮初始化UIButton myButton =[UIButton buttonWithType:UIButtonTypeCustom];(注意此处使用便利构造器初始化的button,不需release)//设置按钮[myButton setBackgroundImage:[UIImage imageNamed:@"myPicpng"] forState:UIControlStateNormal];//设置文字[myButton setTitle:@"确定" forState:UIControlStateNormal];
一、简单说明
一般情况下,点击某个控件后,会做出相应反应的都是按钮
按钮的功能比较多,既能显示文字,又能显示,还能随时调整内部和文字的位置
二、按钮的三种状态
normal(普通状态)
默认情况(Default)
对应的枚举常量:UIControlStateNormal
highlighted(高亮状态)
按钮被按下去的时候(手指还未松开)
对应的枚举常量:UIControlStateHighlighted
disabled(失效状态,不可用状态)
如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
对应的枚举常量:UIControlStateDisabled
三、注意点
(1)从Xcode5开始,资源都放到Imagesxcassets中进行管理,可以使用拖拽的方式添加项目中用到的到Imagesxcassets中
(2)若干多个控件共用一段代码,通常使用tag。
四、代码示例
(1)
复制代码 代码如下:
#import "LFViewControllerh"
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton headImageView;
@end
@implementation LFViewController
// 在OC中,绝大多数的控件的监听方法的第一个参数就是控件本身
//- (IBAction)left:(UIButton )button {
//
// NSLog(@"----");
//}
- (IBAction)move
{
// 通过frame修改head的位置
// 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”
// 允许修改“对象”的'“结构体属性”
// 1 取出结构体属性
CGRect rect = selfheadImageViewframe;
// 2 修改结构体成员
rectoriginy -= 20;
// 3 设置对象的结构体属性
selfheadImageViewframe = rect;
}
(2)
复制代码 代码如下:
#import "LFViewControllerh"
使用git
1 创建项目时,勾选git
2 开发告一段落后,选择"Source Control""Commit",并编写注释
// 枚举类型实质上就是一个整数,作用就是用来替代魔法数字
// 枚举类型中,指定了第一个整数之后,后面的数字会递增
typedef enum
kMovingDirTop = 10,
kMovingDirBottom,
kMovingDirLeft,
kMovingDirRight,
} kMovingDir;
#define kMovingDelta 50
@interface LFViewController ()
@property (weak, nonatomic) IBOutlet UIButton headImageView;
@end
@implementation LFViewController
- (IBAction)move:(UIButton )button
// CGRect rect = selfheadImageViewframe;
CGPoint p = selfheadImageViewcenter;
// magic number魔法数字,其他程序员看到代码的时候,不知道是什么意思
switch (buttontag) {
case kMovingDirTop:
py -= kMovingDelta;
break;
case kMovingDirBottom:
py += kMovingDelta;
break;
case kMovingDirLeft:
px -= kMovingDelta;
break;
case kMovingDirRight:
px += kMovingDelta;
break;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10];
selfheadImageViewcenter = p;
[UIView commitAnimations];
- (IBAction)zoom:(UIButton )button
CGRect rect = selfheadImageViewbounds;
// 在C语言中,关于bool的判断:非零即真
if (buttontag) {
rectsizewidth += 50;
rectsizeheight += 50;
rectsizewidth -= 50;
rectsizeheight -= 50;
// 首尾动画
// beginAnimations表示此后的代码要“参与到”动画中
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:20];
selfheadImageViewbounds = rect;
// selfheadImageViewalpha = 0;
// commitAnimations,将beginAnimation之后的所有动画提交并生成动画
[UIView commitAnimations];
@end
五、补充笔记
1 IBAction的参数
- (IBAction)left:(UIButton )button
(1) 在OC中,绝大多数的控件监听方法的第一个参数就是控件本身
(2) 默认连线时的参数类型是id
(3) 如果要在监听方法中,方便控件的使用,可以在连线时或者连线后,修改监听方法的参数类型
2 修改对象的结构体成员
在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”
修改结构体属性的成员方法如下:
(1)使用临时变量记录对象的结构体属性
(2) 修改临时变量的属性
(3)将临时变量重新设置给对象的结构体属性
3 在程序开发中需要避免出现魔法数字(Magic Number)
使用枚举类型,可以避免在程序中出现魔法数字
(1)枚举类型实质上就是一个整数,其作用就是用来替代魔法数字
(2)枚举类型中,指定了第一个整数之后,后面的数字会递增
4 frame & bounds & center
1> frame可以修改对象的位置和尺寸
2> bounds可以修改对象的尺寸
3> center可以修改对象的位置
5 首尾式动画
复制代码 代码如下:
// beginAnimations表示此后的代码要“参与到”动画中
[UIView beginAnimations:nil context:nil];
// setAnimationDuration用来指定动画持续时间
[UIView setAnimationDuration:20];
selfheadImageViewbounds = rect;
// commitAnimations,将beginAnimation之后的所有动画提交并生成动画
[UIView commitAnimations];
下面来罗列一下UIButton的基本属性罗列
第一、UIButton的定义
复制代码 代码如下:
UIButton button=[[UIButton buttonWithType:(UIButtonType);
能够定义的button类型有以下6种,
复制代码 代码如下:
typedef enum {
UIButtonTypeCustom = 0, 自定义风格
UIButtonTypeRoundedRect, 圆角矩形
UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight, 亮色感叹号
UIButtonTypeInfoDark, 暗色感叹号
UIButtonTypeContactAdd, 十字加号按钮
}UIButtonType;
第二、设置frame
复制代码 代码如下:
button1frame = CGRectMake(20, 20, 280, 40);
[button setFrame:CGRectMake(20,20,50,50)];
第三、button背景色
复制代码 代码如下:
button1backgroundColor = [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填充和背景
复制代码 代码如下:
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
第六、设置button标题和标题颜色
复制代码 代码如下:
[button1 setTitle:@"点击" forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
第七、设置按钮按下会发光
复制代码 代码如下:
buttonshowsTouchWhenHighlighted=NO;
第八、添加或删除事件处理
复制代码 代码如下:
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
第九、 设置按钮内部间距和标题间距
复制代码 代码如下:
UIEdgeInsets insets; // 设置按钮内部间距
insetstop = insetsbottom = insetsright = insetsleft = 10;
btcontentEdgeInsets = insets;
bttitleEdgeInsets = insets; // 标题间距
以上就是关于iOS四大控件全部的内容,包括:iOS四大控件、ios怎样获取uiwebview中button的单击事件、iOS开发 设置button的image的位置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)