UIkit框架之UIbutton的使用

 1.UIbutton的继承关系:UIcontroller:UIview:UIresponder:NSObject;

2.添加按钮的步骤:

    (1)创建按钮的时候首先设置类型

    (2)添加标题或者图片,设置大小来适合按钮内的内容

    (3)为这个按钮链接一个或者多个动作方法

    (4)在界面设置按钮的布局来控制它的大小,位置

    (5)提供有用的信息和局部字符串

3.按钮的类型有:

    (1)UIButtonTypeCustom(没有按钮类型),

    (2) UIButtonTypeSystem(圆角矩形),

    (3)UIButtonTypeDetailDisclosure(详细符号矩形),

    (4)UIButtonTypeInfoLight(背景高光),

    (5)UIButtonTypeInfoDark(背景低暗),

    (6)UIButtonTypeContactAdd(加号按钮),

    (7)UIButtonTypeRoundedRect(圆角矩形,已经使用system的类型来代替)。

4.可以为创建的按钮设置your button’s title (titleEdgeInsets), image (imageEdgeInsets), and both the title and image together (contentEdgeInsets)的属性;但是要注意,设置为info、contact、disclosure类型状态的不应该重新设置这些属性。

5.按钮的状态有:

6.按钮只读的属性:currentTitlecurrentAttributedTitlecurrentTitleColor/imageView

7.可以通过以下属性来设置按钮:

    (1)button.titleLabel.font = [UIFont systemFontOfSize:20]; //设置标题文字的大小

    (2)button.frame = CGRectMake(100, 100, 120, 40); //设置按钮的位置和大小

    (3)[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; //设置题目的颜色,但需要在一定的高光状态下才可以

    (4)[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; //设置题目的阴影颜色,在UIControlStateNormal的状态下才可以进行显示

    (5)[button setTitle:@"button" forState:UIControlStateNormal]; //设置按钮的标题,在正常的状态下就可以显示这个标题

    (6)button setAttributedTitle:setAttributeString forState:UIControlStateDisabled];//设置在不可用的状态下的标题属性

    (7)[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //设置在正常状态下标题的颜色

    (8)button.reversesTitleShadowWhenHighlighted = YES;  //当高亮的时候阴影就会从雕刻形式转变成浮雕的形式

    (9)button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; //当添加的字符超过label行数设定的值时,后面添加的字符就会被截断,这里默认是label只有一行字符

    (10)[button setImage:[UIImage imageNamed:@"DF"] forState:UIControlStateHighlighted];//在高亮的时候显示该图片

    (11)button.adjustsImageWhenHighlighted = YES;  //当按钮高亮的时候按钮上的图片也会变得高亮,,点击后应该就是高亮的状态

    (12)button.adjustsImageWhenDisabled = YES; //当按钮变成灰色的时候显示的图片也会变暗

    (13)button.showsTouchWhenHighlighted = YES; //当按钮被点击的时候按钮就发光变成高亮,但是当按钮有图片和行为的时候就不会有这个功能,默认是no

    (14)[button setBackgroundImage:[UIImage imageNamed:@"Emp"] forState:UIControlStateNormal];//设置在正常状态下的背景图片

    (15)button.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);//修改按钮内的内容到边界的距离,顶,左,底,右

    (16)button.imageEdgeInsets = UIEdgeInsetsMake(100, 20, 50, 20);//修改按钮的图片到边界的距离,注意这不是背景图片

    (17)button.titleEdgeInsets = UIEdgeInsetsMake(30, 20, 20, 20); //修改标题到按钮边界的距离

    (18)button.imageView.exclusiveTouch = YES; //不管按钮显不显示imageview会返回一个布尔值,但system类型的返回的是nil

8.可以通过以下的属性获取按钮的信息

    (1)NSString *titlestring = button.currentTitle; //获取按钮当前的标题

    (2)NSString *statementString = [button titleForState:UIControlStateNormal];//获取在一定状态下的标题

    (3)NSAttributedString *attributeString = [button attributedTitleForState:UIControlStateDisabled];//获取在不可用状态下的标题属性

    (4) UIColor *titleColor = [button titleColorForState:UIControlStateNormal]; //获取在正常状态下标题的颜色

    (5)UIImage *backgroundImage = [button backgroundImageForState:UIControlStateNormal];//获取在正常状态下的背景图片

     (6)UIImage *statementImage = [button imageForState:UIControlStateHighlighted];//获取在高亮状态下的图片

     (7)UIButtonType buttonType = button.buttonType;  //获取按钮的类型,值的类型是NSInteger

     (8)UIColor *currentColor = button.currentTitleColor; //获取当前的标题

       (9) UIColor *currentTitleShadowTitle = button.currentTitleShadowColor; //获取当前标题阴影的颜色

     (10)NSString *titleLabelString =button.titleLabel.text; //获取按钮标题标签的内容,简单点说就是标题

     (11)NSAttributedString *currentAttributeTitle = button.currentAttributedTitle; //获取当前的属性标题

    (12)UIImage *currentBackgroundImage = button.currentBackgroundImage; //获取当前的背景图片

    (13)UIImage *currentImage = button.currentImage; //获取当期前的图片

    (14)button.bounds = [button backgroundRectForBounds:rect]; //返回输入的参数的值

    (15)CGRect contentRectangleBounds = [button contentRectForBounds:button.bounds]; //返回当前按钮内容矩形的位置和大小

    (16)CGRect titleRect = [button titleRectForContentRect:button.bounds]; //返回按钮里面标题矩形的位置和大小

    (17)CGRect imageRectangle = [button imageRectForContentRect:button.bounds]; //返回按钮内图片矩形的位置和大小

原文地址:https://www.cnblogs.com/lelun/p/5676862.html