iOS界面编程-UIButton
2015-10-17 10:39
369 查看
一、介绍
按钮在界面编程中非常常见,从移动端到客户端到pc端软件开发。按钮包含外表设置以及按钮点击事件以及点击过程中按钮状态的改变。在ios中UIbutton 继承自UICotrol类->UIView-UIResponder->NSObject,可以响应用户点击操作,我们还可以设置按钮的标题、按钮的背景图片、按钮的颜色、大小、外表等。
二、相关方法及属性
创建按钮
+ (instancetypenonnull)buttonWithType:(UIButtonType)buttonType
配置按钮标题
1、titleLable 用来显示一个按钮的currentTitle属性的值
@property(nonatomic,
readonly, strong)
UILabel *titleLabel
var titleLabel:
UILabel? { get } 虽然是只读属性,但是她自己的属性却是可以读写的
UIButton *button = [UIButton buttonWithType:
UIButtonTypeSystem];
button.titleLabel.font = [UIFont
systemFontOfSize: 12];
button.titleLabel.lineBreakMode =
NSLineBreakByTruncatingTail;
let
button = UIButton.buttonWithType(.System)
as a href="" UIButton /a
button.titleLabel.font =
UIFont.systemFontOfSize(12)
button.titleLabel.lineBreakMode = .ByTruncatingTail
不要使用label对象去设置文字颜色或者阴影颜色职能通过
- (void)setTitleColor:(UIColor
* nullable)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor
* nullable)color forState:(UIControlState)state
这两个方法去设置不能通过titleLabel.text设置按钮的文字,只能通过- (void)setTitle:(NSString
* nullable)title forState:(UIControlState)state方法去设置
获取相关属性- (NSString
* nullable)titleForState:(UIControlState)state
2、获取及获取相关状态下的属性
- (NSAttributedString
* nullable)attributedTitleForState:(UIControlState)state
- (void)setAttributedTitle:(NSAttributedString
* nullable)title forState:(UIControlState)state
- (UIColor
* nullable)titleColorForState:(UIControlState)state
- (UIColor
* nullable)titleShadowColorForState:(UIControlState)state
3、配置按钮的显示
@property(nonatomic)
BOOL adjustsImageWhenHighlighted 配置是否党按钮高亮时候是否更轻,默认YES
@property(nonatomic)
BOOL adjustsImageWhenDisabled 如果button是disable状态设置按钮颜色更淡
- (UIImage
* nullable)backgroundImageForState:(UIControlState)state
获取相关状态下的背景图片
- (UIImage
* nullable)imageForState:(UIControlState)state设
- (void)setBackgroundImage:(UIImage
* nullable)image forState:(UIControlState)state
4、配置边缘镶嵌
@property(nonatomic)
UIEdgeInsets contentEdgeInsets
@property(nonatomic)
UIEdgeInsets titleEdgeInsets
@property(nonatomic)
UIEdgeInsets imageEdgeInsets
5、获取当前状态
@property(nonatomic,
readonly) UIButtonType
buttonType
@property(nonatomic,
readonly, strong)
NSString *currentTitle
@property(nonatomic,
readonly, strong)
NSAttributedString *currentAttributedTitle
@property(nonatomic,
readonly, strong)
UIColor *currentTitleColor
@property(nonatomic,
readonly, strong)
UIColor *currentTitleShadowColor
6、获取尺寸
- (CGRect)backgroundRectForBounds:(CGRect)bounds
- (CGRect)contentRectForBounds:(CGRect)bounds
- (CGRect)titleRectForContentRect:(CGRect)contentRect
- (CGRect)imageRectForContentRect:(CGRect)contentRect
7 数据类型
typedef enum {
UIButtonTypeCustom =
0,
UIButtonTypeSystem,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypeRoundedRect,
} UIButtonType;
三 、实际例子
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 128, self.view.bounds.size.width - 20, 44);
backButton.titleLabel.font= [UIFont systemFontOfSize:30];
// [backButton setBackgroundColor:[UIColor redColor]];
[backButton setTitle:@"back" forState:UIControlStateNormal];
backButton.titleLabel.text= @"overwrite";
// backButton.reversesTitleShadowWhenHighlighted=YES;
UIImage *blueBImage = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchImage = [blueBImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[backButton setBackgroundImage:stretchImage
forState:UIControlStateNormal];
// backButton.adjustsImageWhenHighlighted=NO;
// backButton.adjustsImageWhenDisabled = YES;
backButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 10);
backButton.titleEdgeInsets = UIEdgeInsetsMake(40, 0, 0, 0);
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton addTarget:self
action:@selector(backButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
CGRect test = [backButton titleRectForContentRect:backButton.bounds];
都是系统提供的方法和属性 ,可以去试试
按钮在界面编程中非常常见,从移动端到客户端到pc端软件开发。按钮包含外表设置以及按钮点击事件以及点击过程中按钮状态的改变。在ios中UIbutton 继承自UICotrol类->UIView-UIResponder->NSObject,可以响应用户点击操作,我们还可以设置按钮的标题、按钮的背景图片、按钮的颜色、大小、外表等。
二、相关方法及属性
创建按钮
+ (instancetypenonnull)buttonWithType:(UIButtonType)buttonType
配置按钮标题
1、titleLable 用来显示一个按钮的currentTitle属性的值
@property(nonatomic,
readonly, strong)
UILabel *titleLabel
var titleLabel:
UILabel? { get } 虽然是只读属性,但是她自己的属性却是可以读写的
UIButton *button = [UIButton buttonWithType:
UIButtonTypeSystem];
button.titleLabel.font = [UIFont
systemFontOfSize: 12];
button.titleLabel.lineBreakMode =
NSLineBreakByTruncatingTail;
let
button = UIButton.buttonWithType(.System)
as a href="" UIButton /a
button.titleLabel.font =
UIFont.systemFontOfSize(12)
button.titleLabel.lineBreakMode = .ByTruncatingTail
不要使用label对象去设置文字颜色或者阴影颜色职能通过
- (void)setTitleColor:(UIColor
* nullable)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor
* nullable)color forState:(UIControlState)state
这两个方法去设置不能通过titleLabel.text设置按钮的文字,只能通过- (void)setTitle:(NSString
* nullable)title forState:(UIControlState)state方法去设置
获取相关属性- (NSString
* nullable)titleForState:(UIControlState)state
2、获取及获取相关状态下的属性
- (NSAttributedString
* nullable)attributedTitleForState:(UIControlState)state
- (void)setAttributedTitle:(NSAttributedString
* nullable)title forState:(UIControlState)state
- (UIColor
* nullable)titleColorForState:(UIControlState)state
- (UIColor
* nullable)titleShadowColorForState:(UIControlState)state
3、配置按钮的显示
@property(nonatomic)
BOOL adjustsImageWhenHighlighted 配置是否党按钮高亮时候是否更轻,默认YES
@property(nonatomic)
BOOL adjustsImageWhenDisabled 如果button是disable状态设置按钮颜色更淡
- (UIImage
* nullable)backgroundImageForState:(UIControlState)state
获取相关状态下的背景图片
- (UIImage
* nullable)imageForState:(UIControlState)state设
- (void)setBackgroundImage:(UIImage
* nullable)image forState:(UIControlState)state
4、配置边缘镶嵌
@property(nonatomic)
UIEdgeInsets contentEdgeInsets
@property(nonatomic)
UIEdgeInsets titleEdgeInsets
@property(nonatomic)
UIEdgeInsets imageEdgeInsets
5、获取当前状态
@property(nonatomic,
readonly) UIButtonType
buttonType
@property(nonatomic,
readonly, strong)
NSString *currentTitle
@property(nonatomic,
readonly, strong)
NSAttributedString *currentAttributedTitle
@property(nonatomic,
readonly, strong)
UIColor *currentTitleColor
@property(nonatomic,
readonly, strong)
UIColor *currentTitleShadowColor
6、获取尺寸
- (CGRect)backgroundRectForBounds:(CGRect)bounds
- (CGRect)contentRectForBounds:(CGRect)bounds
- (CGRect)titleRectForContentRect:(CGRect)contentRect
- (CGRect)imageRectForContentRect:(CGRect)contentRect
7 数据类型
typedef enum {
UIButtonTypeCustom =
0,
UIButtonTypeSystem,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
UIButtonTypeRoundedRect,
} UIButtonType;
三 、实际例子
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 128, self.view.bounds.size.width - 20, 44);
backButton.titleLabel.font= [UIFont systemFontOfSize:30];
// [backButton setBackgroundColor:[UIColor redColor]];
[backButton setTitle:@"back" forState:UIControlStateNormal];
backButton.titleLabel.text= @"overwrite";
// backButton.reversesTitleShadowWhenHighlighted=YES;
UIImage *blueBImage = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchImage = [blueBImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[backButton setBackgroundImage:stretchImage
forState:UIControlStateNormal];
// backButton.adjustsImageWhenHighlighted=NO;
// backButton.adjustsImageWhenDisabled = YES;
backButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 10);
backButton.titleEdgeInsets = UIEdgeInsetsMake(40, 0, 0, 0);
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 0, 0);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton addTarget:self
action:@selector(backButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
CGRect test = [backButton titleRectForContentRect:backButton.bounds];
都是系统提供的方法和属性 ,可以去试试
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 100 个最佳 Ubuntu 应用(中)
- 在 AppImage、Flathub 和 Snapcraft 平台上搜索 Linux 应用
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 24 个必备的 Linux 应用程序
- 注册表趣味应用小集
- 远程控制技术的应用
- 路由器访问列表的应用
- xDSL技术及其应用
- 基于XML的桌面应用
- SQL Server 2008 R2 应用及多服务器管理
- winform异型不规则界面设计的实现方法
- ExtJS 2.0实用简明教程之应用ExtJS
- JSP应用的安全问题
- Android中设置只有程序第一次运行才显示的界面实现思路