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 //为框架预留的
四、设置按钮形状类型
- self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- buttonWithType: 定义button按钮的外形
- 六种定义button类型: 下面有图解
- UIButtonTypeCustom = 0, 无类型
- UIButtonTypeRoundedRect, 四个角是圆弧 型的
- UIButtonTypeDetailDisclosure,
- UIButtonTypeInfoLight,
- UIButtonTypeInfoDark,
- 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按钮的详解的更多相关文章
- [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- IOS中UITableViewCell使用详解
IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...
- iOS 单元测试之XCTest详解(一)
iOS 单元测试之XCTest详解(一) http://blog.csdn.net/hello_hwc/article/details/46671053 原创blog,转载请注明出处 blog.csd ...
- [转]iOS学习之UINavigationController详解与使用(三)ToolBar
转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...
- IOS 友盟使用详解
IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
随机推荐
- Django实战(17):ajax !
现在让我们来通过ajax请求后台服务.当然首选要实现后台服务.关于“加入购物车”,我们需要的服务是这样定义的: url: http://localhost:8000/depotapp/API/c ...
- Hive分区和桶的概念
Hive 已是目前业界最为通用.廉价的构建大数据时代数据仓库的解决方案了,虽然也有 Impala 等后起之秀,但目前从功能.稳定性等方面来说,Hive 的地位尚不可撼动. 其实这篇博文主要是想聊聊 S ...
- thinkphp和ueditor自定义后台处理方法整合
先了解一下ueditor后台请求参数与返回参数格式规范: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...
- 《Android源码设计模式》--策略模式
No1: 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. No2: 使用场景: 1)针对同一类型问题的多种处理方式,仅 ...
- DRUID控制
@Configuration public class DruidConfiguration { @Bean public ServletRegistrationBean statViewServle ...
- Java 中的三大特性
我们都知道 Java 中有三大特性,那就是继承 ,封装和多态 .那我今天我就来说说这几个特性 . 老样子 ,先问问自己为什么会存在这些特性 .首先说封装 ,封装就是使用权限修饰符来实现对属性的隐藏 , ...
- 手动制作用于启动BeagleBoneBlack(am335x)的SD
1.需求MLO.u-boot.img.uImage.Systemfile 获取MLO.u-boot.img下载U-boot源码,解压获得源码,进入源码目录 cd /home/zyr/Source_co ...
- 【SQL】181. Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- 深入理解ajax系列第四篇
前面的话 现代Web应用中频繁使用的一项功能就是表单数据的序列化,XMLHttpRequest 2级为此定义了FormData类型.FormData为序列化表单以及创建与表单格式相同的数据提供了便利. ...
- [ 原创 ] Java基础6--构造函数和抽象类的性质
构造函数的性质 // A.方法名与类名相同: // B.没有返回类型(例如return.void等):// C.不能被static.final.native.abstract和synchronized ...