UIKit之浅析UIButton
UIButton * button =[[UIButton alloc]init];
button.backgroundColor=[UIColor redColor];
[button setTitle:@"我是button" forState:UIControlStateNormal];
button.frame = CGRectMake(, , , ) ;
[self.view addSubview:button];

可能有时候,想让title不在中间或者靠某一边,现在就举个例子怎么把文字在button的底部。
代码:
button.contentEdgeInsets = UIEdgeInsetsMake(, , , );
然后看一下 视图是这个样子:

这里边用到
button.contentEdgeInsets = UIEdgeInsetsMake(100, 0, 0, 0); //UIEdgeInsetsMake(距离top的距离, 距离left的距离, 距离botton的距离, 距离right的距离)
//设置 button的内容的便宜量
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // 这是内容的偏移量
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // title的偏移量
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // image的偏移量
@property(nonatomic) BOOL adjustsImageWhenHighlighted; // default is YES. if YES, image is drawn darker when highlighted(pressed)当高亮的时候if yes 图片自适应
@property(nonatomic) BOOL adjustsImageWhenDisabled; // default is YES. if YES, image is drawn lighter when disabled
@property(nonatomic) BOOL showsTouchWhenHighlighted; // 当高亮的时候if yes 图片自适应
@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0); //高亮颜色 @property(nonatomic,readonly) UIButtonType buttonType; //button的类型
[button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
//设置文字的边框颜色

不太明显,效果还是有的。
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = , // no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // standard system button
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark, //
UIButtonTypeContactAdd, //按钮上面是哥加号
UIButtonTypeRoundedRect = UIButtonTypeSystem, //系统默认的
};
创建button的另一中写法:
button =[UIButton buttonWithType:UIButtonTypeInfoDark];
- (void)setTitle:(NSString *)title forState:(UIControlState)state; // 设置button的title
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置buttontitle的颜色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black设置title的字体的阴影颜色
- (void)setImage:(UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states 设置button的image 图片 大小是图片的真实大小
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil 设置图片的背景 image会铺满button
- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line 设置 自定义的字符串为button的title
- (NSString *)titleForState:(UIControlState)state; // 获取某个状态的title
- (UIColor *)titleColorForState:(UIControlState)state; //获取某个状态的title的字体颜色
- (UIColor *)titleShadowColorForState:(UIControlState)state; //获得某个状态的阴影颜色
- (UIImage *)imageForState:(UIControlState)state; //获得某个状态的image
- (UIImage *)backgroundImageForState:(UIControlState)state; //获得背景图片
- (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0); //获得自定义字符串
@property(nonatomic,readonly,retain) NSString *currentTitle; // normal/highlighted/selected/disabled. can return nil 正常获取当前的title
@property(nonatomic,readonly,retain) UIColor *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1) 获取当前title的颜色
@property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor; // normal/highlighted/selected/disabled. default is white(0,0.5).获取当前的阴影颜色
@property(nonatomic,readonly,retain) UIImage *currentImage; // normal/highlighted/selected/disabled. can return nil 获取当前的imager
@property(nonatomic,readonly,retain) UIImage *currentBackgroundImage; // normal/highlighted/selected/disabled. can return nil 获取当前的背景image
@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // normal/highlighted/selected/disabled. can return nil // return title and image views. will always create them if necessary. always returns nil for system buttons
@property(nonatomic,readonly,retain) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); 获取content的view 就是一个lable
@property(nonatomic,readonly,retain) UIImageView *imageView NS_AVAILABLE_IOS(3_0); imageView 就是button的image的试图
(CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
- (CGRect)imageRectForContentRect:(CGRect)contentRect;
@end @interface UIButton(UIButtonDeprecated) @property(nonatomic,retain) UIFont *font NS_DEPRECATED_IOS(2_0, 3_0);//通过这个可以给title设置字体
@property(nonatomic) NSLineBreakMode lineBreakMode NS_DEPRECATED_IOS(2_0, 3_0);// 换行模式
@property(nonatomic) CGSize titleShadowOffset NS_DEPRECATED_IOS(2_0, 3_0); // title的偏移量 @end
UIKit之浅析UIButton的更多相关文章
- UIkit框架之UIbutton的使用
1.UIbutton的继承关系:UIcontroller:UIview:UIresponder:NSObject: 2.添加按钮的步骤: (1)创建按钮的时候首先设置类型 (2)添加标题或者图片,设置 ...
- UIButton利用分类扩展方法(封装)
UIButton+BackgroundColor.h #import <UIKit/UIKit.h> @interface UIButton (BackgroundColor) - (vo ...
- iOS学习——UI基础UIButton(七)
前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件 ...
- UI基础UIButton
UI基础UIButton 前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实 ...
- iOS:UIButton扩大按钮的响应区域
一.介绍 在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大UIButton的响应范围 二.方式 下面有两个解决办法: 第一种方法:创建一个类目:UIButton+EnlargeTouch ...
- Swift重写UIButton的图片和标题的位置
import UIKit class ResetBtn: UIButton { let IMAGE_RATIO :CGFloat = 0.7 // 图片占整个按钮高度的比例 let TITLE_FON ...
- iOS之UIButton扩大按钮的响应区域
在开发中有时会遇见设计图里按钮设计的特别小,这时会用到手动扩大UIButton的响应范围,下面有两个解决办法: 第一种方法:创建一个类目:UIButton+EnlargeTouchArea .h文件 ...
- iOS学习笔记(七)——UI基础UIButton
前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件 ...
- UIButton 加载网络图片
以后就可以 用这个分类 UIButton轻松加载网络图片了, UIButton+WebCache.h #import <UIKit/UIKit.h> @interface UIButt ...
随机推荐
- oracle系统参数修改
create pfile='/home/oracle/sss.ora' from spfile; create spfile from pfile='/home/oracle/sss.ora'; al ...
- 网站建设之Django搭建与配置
总是忘记一些问题解决的细节,终于发现做笔记的必要了.一步一步慢慢写,慢慢积累吧.从开始接触计算机,从硬件到系统到软件,遇到的问题真心不算少了,都记下来的话也得有本书厚了. Linux Version: ...
- ORM和Core
.net core中有哪些被抛弃的类 1.DataTable DataRow SqlDataAdapter DataRow DataColumn DataColumn 虽然这些类不是我ORM核心功能 ...
- HTML5新属性-----拖放
最早引入JS拖放功能的是IE4,当时,网页中只有两种对象可以拖放:图像和某些文本.拖动图像时,把鼠标放在图像上,按住鼠标不放就可以拖动它.拖动文本时,需要选中文本,然后可以像拖动图像一样拖动被选中的文 ...
- Android07-Android广播
¨广播机制简介 ¨动态和静态注册广播 ¨接收系统广播 ¨自定义广播 ¨本地广播 n广播是一种系统级的事件(Event). n当系统发生某些事件时(例如,收到短信.来电.电量低),就会向整个Andr ...
- ThinkPHP框架下基于RBAC的权限控制模式详解
这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...
- jdk1.6,jdk1.7共存
当然可以,安装的时候记得选择不同的安装目录,安装好以后,可以在开发工具(如eclipse)中切换不同的编译环境和运行环境.其实只要安装eclipse就自带了jdk1.3-1.6的编译环境了. Mac下 ...
- 关于css中使用ul li的一些体会
参考网址:http://hi.baidu.com/july_leo/item/5237cd612070ae2668105b40 如何修改ul li的显示 ----------------------- ...
- Codeforces 325D
#include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...
- Android 打包签名 从生成keystore到完成签名
进入生成工具: 工具帮助: 输入指令并获得结果: 转自: http://www.cppblog.com/fwxjj/archive/2010/05/24/116208.html 首先,我们需 ...