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);

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

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

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

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

    iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...

  3. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  4. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  5. IOS中UITableViewCell使用详解

    IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...

  6. iOS 单元测试之XCTest详解(一)

    iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...

  7. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  8. IOS 友盟使用详解

    IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...

  9. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

随机推荐

  1. printf的定义

    1. printf的宏定义 #define XXX_ERROR 0#define XXX_WARNING 1#define XXX_INFO 2#define XXX_DEBUG 3#define X ...

  2. PHP中双引号引起的命令执行漏洞

    前言 在PHP语言中,单引号和双引号都可以表示一个字符串,但是对于双引号来说,可能会对引号内的内容进行二次解释,这就可能会出现安全问题. 正文 举个简单例子 <?php $a = 1; $b = ...

  3. PHP接入支付宝支付

    创建应用 使用支付宝账号登录开放平台创建应用,应用创建成功之后可以得到APPID等相关信息 接着需要设置RSA密钥,可以使用蚂蚁金服开放平台提供的生成工具,生成完密钥需在开放平台中填写. 代码接入 引 ...

  4. CSUOJ 1895 Apache is late again

    Description Apache is a student of CSU. There is a math class every Sunday morning, but he is a very ...

  5. 使用Mongo索引需要注意的几个点

    1.正则表达式和取反运算符不适合建立索引 正则表达式:$regex 取反运算符:$ne ,$nin 2.backgroud建立索引速度缓慢 前台创建是会有阻塞,backgroud效率缓慢,实际情况实际 ...

  6. [ 转载 ] Java基础12--基础学习总结——数组

    java基础学习总结——数组 一.数组的基本概念 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量. 数组的 ...

  7. [BZOJ4699]树上的最短路(最短路+线段树)

    https://www.cnblogs.com/Gloid/p/10273902.html 这篇文章已经从头到尾讲的非常清楚了,几乎没有什么需要补充的内容. 首先$O(n\log^2 n)$的做法比较 ...

  8. [Luogu5162]WD与积木(多项式求逆)

    不要以为用上Stirling数就一定离正解更近,FFT都是从DP式本身出发的. 设f[i]为i个积木的所有方案的层数总和,g[i]为i个积木的方案数,则答案为$\frac{f[i]}{g[i]}$ 转 ...

  9. BZOJ 1174 [Balkan2007]Toponyms(Trie)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1174 [题目大意] 选出一些字符串,使得字符串的最长公共前缀*字符串的总个数最大化 [ ...

  10. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...