iOS:UIButton按钮的详解

UIButton的详细介绍:

一、按钮具有的属性:

@property(nonatomic,readonly) UIButtonType buttonType;  //按钮形状类型

@property(nonatomic,readonly,retain) NSString *currentTitle;    //按钮当前文字

@property(nonatomic,readonly,retain) UIColor  *currentTitleColor;     //按钮当前文字颜色

@property(nonatomic,readonly,retain) UIColor  *currentTitleShadowColor;  //按钮文字当前阴影颜色

@property(nonatomic,readonly,retain) UIImage  *currentImage;             //按钮当前前景图片

@property(nonatomic,readonly,retain) UIImage  *currentBackgroundImage;    //按钮当前背景图片

@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle //按钮文字当前属性

@property(nonatomic,readonly,retain) UILabel     *titleLabel    //按钮标签

@property(nonatomic,readonly,retain) UIImageView *imageView  //按钮视图

@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;    //按钮垂直放置方式

@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //按钮水平放置方式

@property(nonatomic,readonly) UIControlState  //按钮状态类型

二、设置按钮的属性值

- (void)setTitle:(NSString *)title forState:(UIControlState)state;   //设置按钮文字内容

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state  //设置按钮文字颜色

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state  //设置按钮文字阴影颜色

- (void)setImage:(UIImage *)image forState:(UIControlState)state;   //设置按钮前景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state  //设置按钮背景图片

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state  //设置按钮文字属性

三、按钮的状态类型

按钮类型UIControlState:

UIControlStateNormal          //正常类型

UIControlStateHighlighted    //高亮类型

UIControlStateDisabled       //禁用类型

UIControlStateSelected       //选中类型

UIControlStateApplication   //当应用程序标识使用时

UIControlStateReserved      //为框架预留的

四、设置按钮形状类型

  1. self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  2. buttonWithType:  定义button按钮的外形
  3. 六种定义button类型: 下面有图解
  4. UIButtonTypeCustom = 0,    无类型
  5. UIButtonTypeRoundedRect,    四个角是圆弧   型的
  6. UIButtonTypeDetailDisclosure,
  7. UIButtonTypeInfoLight,
  8. UIButtonTypeInfoDark,
  9. UIButtonTypeContactAdd,

或者:

[Btn.layer setMasksToBounds:YES];
    [Btn.layer setCornerRadius:8.0]; //设置矩圆角半径
    [Btn.layer setBorderWidth:1.0];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
    [Btn.layer setBorderColor:colorref];//边框颜色

五、获取按钮的属性

- (NSString *)titleForState:(UIControlState)state;      //获取按钮文字

- (UIColor *)titleColorForState:(UIControlState)state;  //获取按钮文字颜色

- (UIColor *)titleShadowColorForState:(UIControlState)state; //获取按钮文字阴影颜色

- (UIImage *)imageForState:(UIControlState)state; //获取按钮前景图片

- (UIImage *)backgroundImageForState:(UIControlState)state; //获取按钮背景图片

- (NSAttributedString *)attributedTitleForState:(UIControlState)state; //获取按钮文字属性

六、按钮文字放置方式

垂直放置:

UIControlContentVerticalAlignmentCenter   //居中

UIControlContentVerticalAlignmentTop       //置顶

UIControlContentVerticalAlignmentBottom   //置底

UIControlContentVerticalAlignmentFill        //填充

水平放置:

UIControlContentHorizontalAlignmentCenter  //居中

UIControlContentHorizontalAlignmentLeft     //居左

UIControlContentHorizontalAlignmentRight   //居右

UIControlContentHorizontalAlignmentFill      //填充

说明:

(1) 由于按钮有状态类型之分,所以,在给按钮添加文字时,使用button.TitleLabel.Text = @“按钮”这种赋值方式是无效的,在视图中不会显示出来,应该使用[button setTitle:(NSString *)title forState:(UIControlState)

state]这种方式才是有效地。同样设置文字的颜色也是如此:

设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

[btn.titleLabel setTextColor:[UIColorblackColor]];

btn.titleLabel.textColor=[UIColor redColor];

而是用:

[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

(2)获取按钮的文字,应该使用[button currentTitle],如果使用button.titleLabel.Text,其结果并不是你设置的文字内容。同样获取文字的颜色也是如此.[button currentTitleColor]

(3)设置按钮上的字体的大小

[button setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在     将来的SDK版本中去除改方法

button.titleLabel.font = [UIFont fontWithName:(NSString*)fontName size:14.0]; //应该使用

或者

button.TitleLabel.font = [UIFont systemFontOfSize: 14.0];    //应该使用

(4) 有些时候我们想让UIButton的title居左对齐

button.textLabel.textAlignment = UITextAlignmentLeft  //是没有作用的,我们需要设置

button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; //显示居左

但是问题又出来,文字会紧贴到做边框,我们可以设置使文字距离左边框保持10个像素的距离。

button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

时间: 2024-08-23 13:14:58

iOS:UIButton按钮的详解的相关文章

iOS UIButton的使用详解

button的创建方法有: //对象方法初始化 UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 75)]; //类方法初始化 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //设置frame btn1.frame = CGRectMake(100, 100, 100, 75); btn1.backgroundCo

Objective-C:UIButton按钮的详解

UIButton的详细介绍: 一.按钮具有的属性: @property(nonatomic,readonly) UIButtonType buttonType;  //按钮类型 @property(nonatomic,readonly,retain) NSString *currentTitle;    //按钮当前文字 @property(nonatomic,readonly,retain) UIColor  *currentTitleColor;     //按钮当前文字颜色 @proper

iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

1.RootView 跳到SecondView 首先我们需要新一个View.新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView 2.为Button 添加点击事件,实现跳转 在RootViewController.xib中和RootViewController.h文件建立连接 在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationCon

[转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. 1.RootView 跳到SecondView 首先我们需要新一个View.新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView 2

iOS定位服务编程详解

现在的移动设备很多都提供定位服务,使用iOS系统的iPhone.iPod Touch和iPad都可以提供位置服务,iOS设备能提供3种不同途径进行定位:Wifi, 蜂窝式移动电话基站, GPS卫星 iOS 不像Android系统在定位服务编程时,可以指定采用哪种途径进行定位.iOS的API把底层这些细节屏蔽掉了,开发人员和用户并不知道现在设备是采用 哪种方式进行定位的,iOS系统会根据设备的情况和周围的环境,采用一套最佳的解决方案.这个方案是这样的,如果能够接收GPS信息,那么设备优先采用 GP

(转) IOS ASI http 框架详解

(转) IOS ASI http 框架详解 ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRequest适用于基本的HTTP请求,和基于REST的服务之间的交互. ASIHTTPRequest功能很强大,主要特色如下: l 通过简单的接口,即可完成向服务端提交数据和从服务端获取数据的工作 l 下载的数据,可存储到内存中或直接存储到磁盘中 l 能上传

IOS—UITextFiled控件详解

IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderS

iOS 中 NSTimer 使用详解-北京尚学堂

iOS 中 NSTimer 使用详解-北京尚学堂 前阵子在整理公司项目的时候,发现老代码在使用 NSTimer 时出现了内存泄露.然后整理了一些 NSTimer 的相关内容.比较简单,各位见笑啦. NSTimer fire 我们先用 NSTimer来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface DetailViewController () @property (nonatomic, weak) NSTimer *timer; @end

IOS中UITableViewCell使用详解

IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier; Cell的初始化方法,可以设置一个风格和标识符,风格的枚举如下: ? 1 2 3 4 5 6 typedef NS_ENUM(NSInteger, UITableViewCellStyle) {     UITableViewCellStyleDe