iOS UI基本控件

控件一. UILabel

 1》description:

UILabel继承于UIView, 是UIView的子类。所以UILabel拥有UIView 的所有属性和方法。

UILabel在UI开发中的主要作用: 显示文本信息。在API中被创建的UILabel对象是无法参与用户交互机制,如果你想让你创建的UILabel对象参与用户交互的话,你需要修改userInteractionEnabled属性设置为YES。

//    label和imageView都要开用户交互

inputLabel.userInteractionEnabled = YES;

2》初始化

UILabel对象的初始化

UILabel没有自己的初始化方法,所以我们可以调用其父类的初始化

- (instancetype)initWithFrame:(CGRect)aRect//通过给label一个坐标(x,y)和大小(width,height)

UILabel *inputLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 110)];

3》属性

UILabel是用来显示文本信息的空间

@property(nonatomic, copy) NSString *text

属性text:可以直接给label附一个字符串并让其字符串在label上显示

label.text = @"I'm a label, I can show string"; 

label.text = [NSString stringWithFormat:<#(nonnull NSString *), ...#>]

@property(nonatomic, strong) UIFont *font

 属性font:可以调整label上显示文本的字体格式(可以修改字体的大小和字体格式)

label.font = [UIFont systemFontOfSize:40];

label.font = [UIFont fontWithName:<#(nonnull NSString *)#> size:<#(CGFloat)#>]

@property(nonatomic, strong) UIColor *textColor

属性textColor:可以调整label上显示文本的颜色

label.textColor = [UIColor lightGrayColor];

@property(nonatomic) NSTextAlignment textAlignment

属性textAlignment:label上显示文本的对齐方式(有三个值:1.NSTextAlignmentLeft做对齐“默认是左对齐”2.NSTextAlignmentCenter居中对齐3.NSTextAlignmentRight右对齐)

label.textAlignment = NSTextAlignmentCenter;

@property(nonatomic) NSInteger numberOfLines;

属性:numberOfLines表示在label上显示的文本将要以多少行显示在label上,若果你不知道显示在label上的文本回显示多少行久给该属性赋值为0,这样系统会自己根据lanbel的大小和文本字体大小来自己计算显示多少行。

@property(nonatomic, strong) UIColor *shadowColor

@property(nonatomic) CGSize shadowOffset

label 的shadowColor和shadowOffset属性通常在一起使用,设置label上文字的阴影颜色和阴影大小(通常用在艺术字和炫酷的文字效果的场景下使用)

// 7. 阴影 shadowColor

label.shadowColor = [UIColor blackColor];

label.shadowOffset = CGSizeMake(2, 1);

效果如下:


iOS UI基本控件_第1张图片

@property(nonatomic) BOOL clipsToBounds

属性clipsToBounds:一个布尔值,确定子视图的范围仅限于视图,子试图超出父试图的那部分会被隐藏起来,默认值是NO。通过是在给label做圆角效果的时候使用。

inputLabel.layer.cornerRadius = 4;//通过label的layer图层改变label的圆角

inputLabel.clipsToBounds = YES;//设置label的clipsToBounds属性并赋值为YES

[inputLabel addSubview:passwordImageView];//将passwordImageView添加到其父试图inputLabel上,如果passwordImageView的大小超过父试图inputLabel大小的部分会被隐藏。

效果如下:

控件二. UIButton

1》description:

UIButton是一个按钮的控件类,在平时的开发中用到的非常之多,无论什么APP或者其他应用中button这个空间是必不可少的,它是一切事件响应开始的主要控件之一。UIButton类继承UIControl类,而UIControl类继承UIView类。所以UIButton也同样继承了UIView的所有属性和方法。

UIButton的外观可以自定制,但要自定制UIButton的外观需要了解UIButton是由什么构成的,UIButton由三个子控件构成分别是UIImage(button的左边),UILabel(button的右边),BackGroundImage(button的背景)如图:

iOS UI基本控件_第2张图片

注:这些子控件如果你不设置是没有的。

2》初始化

UIButton没有自己的初始化方法,其初始化方法可以用父类UIView的初始化方法。

但UIButton有自己的类方法。

+ (instancetype)buttonWithType:(UIButtonType)buttonType

构造器方法:有多重buttonType:

UIButtonTypeCustom = 0,

UIButtonTypeSystem,

UIButtonTypeDetailDisclosure,

UIButtonTypeInfoLight,

UIButtonTypeInfoDark,

UIButtonTypeContactAdd,

UIButtonTypeRoundedRect,

2》属性

@property(nonatomic, readonly, strong) UILabel *titleLabel

属性:titleLabel:button上显示文本信息

button.titleLabel.text = @"i'm a label";

@property(nonatomic, readonly, strong) UIImage *currentImage

属性:currentImage button上显示image

button.currentImage = [UIImage imageName:@"o1.png"];

@property(nonatomic, readonly, strong) UIImage *currentBackgroundImage

属性:currentBackgroundImage:button上显示背景图片

button.currentBackgroundImage =  [UIImage imageName:@"o1.png"];

3》方法

- (void)setTitle:(NSString *)title forState:(UIControlState)state

设置button上显示的文字

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state

设置button上显示文字的颜色

- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

UIcontrol的重要方法之一,给button 添加点击事件:给button添加点击事件 target :目标对象, action:选择器识别一个动作信息。它不能为空。

controlEvents:控制事件的行动消息发送.

控件三:UITextField

1》description:

UITextField通常称为编辑框和输入框继承UIControl 说明UITextField也具有事件机制

1》属性

@property(nonatomic, copy) NSString *text

属性:text:textField一个默认的显示在编辑框的文字

@property(nonatomic, copy) NSString *placeholder

属性:placeholder:textField的一个占位符

textField.placeholder = @"请输入用户名";

效果如下:

@property(nonatomic) BOOL clearsOnBeginEditing

属性:clearsOnBeginEditing:当每次开始编辑textField时清除上次编辑的文本

@property(nonatomic) UITextFieldViewMode clearButtonMode

属性:clearButtonMode:出现在TextField编辑框最右边的一个小叉按钮,作用就是可以点击清除所编辑的文本。

textField.clearButtonMode = UITextFieldViewModeAlways;

清除按钮共有四种选项分别在不同的条件下显示清除按钮:(

UITextFieldViewModeNever,

UITextFieldViewModeWhileEditing,

UITextFieldViewModeUnlessEditing,

UITextFieldViewModeAlways

效果如下:


iOS UI基本控件_第3张图片

@property(nonatomic, copy) NSString *text

属性text:用的很多不仅可以给textField赋值还可以从textField取值。大多数我们做登录注册和搜索引擎时都需要获取用户在textField上输入的值我们就可以通过textField的text属性获取我们需要的值。

控件四:UIImageView

1》description

UIImageView顾名思义就是存放图片的View相当于相框的一个作用,UIImageView继承与UIView拥有UIView所有的属性和方法。

1》初始化

- (instancetype)initWithImage:(UIImage *)image

通过一个给定的UIImage的初始化方法

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

- (instancetype)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage

初始化时分别在Nomal情况下和highlighted状态下的两个image进行初始化

@property(nonatomic, strong) UIImage *image

属性:image :通过image属性也能给ImageView添加图片

@property(nonatomic, copy) NSArray*animationImages

属性:animationImages的类型是一个NSArray可以知道是一个存放N个图片的数组,通常用作存放动画的图片组。

@property(nonatomic) NSTimeInterval animationDuration

属性:animationDuration动画播放一周期的时长。

3》方法

- (void)startAnimating

播放动画

- (void)stopAnimating

停止播放动画

你可能感兴趣的:(iOS UI基本控件)