iOS--玩转UILabel and UIButton的使用技巧
在学习ios开发时候,很多小伙伴接触到第一个就是HelloWorld,让我们今天通过label(标签)和button(按钮)来实现HelloWorld。
UILabel使用方法
在Xcode中的对象控件中提供了UILabel和UIButton控件,我们可以直接选择拖动(control+鼠标拖动)到interface接口下,默认操作为定义标签的输入输出口,并且格式为@property (nonatmic,weak) IBOutlet UILabel* label;
一.使用控件来显示标签
@property (nonatmic,weak) IBOutlet UILabel* lable;
其中weak类型为弱引用,只在self.view视图中lable对象不持有对象的所有权,当对象使用完成后对象会被立刻销毁,并被设置为nil。
图1--创建输入输出接口.png
二.使用代码来显示标签
俗话说,“控件能做的代码无所不能”--随便说说,让我们来看一下如何通过代码来实现标签
//创建一个UILable标签
CGRect labelLocation = CGRectMake(130, 250, 150, 20); //用来显示标签坐标
self.label = [[UILabel alloc] initWithFrame:lableLocation];
self.label.text = @"HelloWorld"; //设置文本内容
self.label.textAlignment = NSTextAlignmentCenter; //文本位置
[self.view addSubview:self.label]; //添加子视图到父视图
代码解析
1.lableLocation通过CGRect创建一个标签坐标,CGRectMake用来设置x,y,width,height(参考上期bounds and frame内容)
- self.lable.textAlignment = NSTextAlignmentCenter; //文本位置
/* Values for NSTextAlignment */
typedef NS_ENUM(NSInteger, NSTextAlignment) {
NSTextAlignmentLeft = 0, // Visually left aligned
if TARGET_OS_IPHONE && !0
NSTextAlignmentCenter = 1, // Visually centered
NSTextAlignmentRight = 2, // Visually right aligned
else /* !TARGET_OS_IPHONE */
NSTextAlignmentRight = 1, // Visually right aligned
NSTextAlignmentCenter = 2, // Visually centered
endif
NSTextAlignmentJustified = 3, // Fully-justified. The last line in a paragraph is natural-aligned.
NSTextAlignmentNatural = 4, // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);
3.在通过代码创建UI标签不同的一点是,使用@property (nonatmic,strong) UILable* lable;
此时是直接强引用使用标签,不同于通过控件设置,如果在控件中使用强引用则导致循环死锁,相互强引用无法释放!
程序运行如下
图2--标签实现图UIButton实现部分
同lable标签同样使用代码来实现(此处省略控件设置按钮)
示例代码
//创建一个UIButton按钮
CGRect buttonLocation = CGRectMake(130, 300, 150, 20);
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = buttonLocation;
//添加按钮动作
[button addTarget:self action:@selector(changeTheText:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
创建UIButton的细节:
1.先声明一个button对象,并初始化其button类型(结构体)
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypePlain API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios, watchos), // standard system button without the blurred background view
UIButtonTypeRoundedRect = UIButtonTypeSystem // Deprecated, use UIButtonTypeSystem instead
};
2.初始化button的位置,同lable相同
3.添加到父视图当中
添加button的响应时间 【前文提及,button属于控件类,有能力响应用户的事件】
代码如下
//添加按钮动作
[button addTarget:self action:@selector(changeTheText:) forControlEvents:UIControlEventTouchUpInside];
选择器内容为
- (void)changeTheText:(id)sender{
//NSLog(@"change the text.");
CGRect lableNewLocation= CGRectMake(self.lable.bounds.origin.x, self.lable.bounds.origin.y
, self.lable.bounds.size.width+30, self.lable.bounds.size.height);
self.lable.text = @"I'm a Objective-C programer.";
self.lable.bounds = lableNewLocation;}
通过改变self.lable文本内容作为此按钮的选择器,UIControlEventTouchUpInside表示点击抬起后的事件响应方式
其他响应方式如下:
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
};
最终程序运行结果
图3--程序初始化页面 图4--响应按钮事件后的页面通过对于UILabel and UIButton的使用,可以设计自己的第一个程序
总结
UILabel 非控制类 可以使用代码直接实现,同时与对象控件下的标签产生的输入输出接口应该注意引用类型
UIButton 控制类 可以使用代码直接实现,注意一定要初始化按钮类型,并且响应机制的选择,可以自定义按钮的图案,默认是系统类型。