UIButton
来源:互联网 发布:名片设计软件手机版 编辑:程序博客网 时间:2024/06/03 14:00
概述:
UIButton 类提供了按钮功能(将触摸按钮的状态信息发送给目标对象). UIButton的对象共有三种状态: normal(正常状态)/ selected(按下去时的状态)/ highlighted(被选中的状态).同时 UIButton 也提供了多种系统样式
1.1 属性方法
配置 title
@property(nonatomic, readonly, retain) UILabel *titleLabel
- (NSString *)titleForState:(UIControlState)
state
- (void)setTitle:(NSString *)
title
forState:(UIControlState)
state
- (NSAttributedString *)attributedTitleForState:(UIControlState)
state
- (void)setAttributedTitle:(NSAttributedString *)
title
forState:(UIControlState)
state
- (UIColor *)titleColorForState:(UIControlState)
state
- (void)setTitleColor:(UIColor *)
color
forState:(UIControlState)
state
- (UIColor *)titleShadowColorForState:(UIControlState)
state
- (void)setTitleShadowColor:(UIColor *)
color
forState:(UIControlState)
state
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted
现实的文本
在设定状态下,设置title
在设定状态下设置属性title
设置字体颜色
设置字体阴影
设置阴影颜色
配置 button的界面显示
@property(nonatomic) BOOL showsTouchWhenHighlighted
- (UIImage *)backgroundImageForState:(UIControlState)
state
- (UIImage *)imageForState:(UIControlState)
state
- (void)setBackgroundImage:(UIImage *)
image
forState:(UIControlState)
state
- (void)setImage:(UIImage *)
image
forState:(UIControlState)
state
@property(nonatomic, retain) UIColor *tintColor
按下按钮时, 按钮会闪一下
设置指定状态的图片
配置缩进
@property(nonatomic) UIEdgeInsets contentEdgeInsets
@property(nonatomic) UIEdgeInsets titleEdgeInsets
@property(nonatomic) UIEdgeInsets imageEdgeInsets
内容缩进
标题缩进
图片缩进
获取当前的状态
@property(nonatomic, readonly) UIButtonType buttonType
@property(nonatomic, readonly, retain) NSString *currentTitle
@property(nonatomic, readonly, retain) NSAttributedString*currentAttributedTitle
@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) UIImageView *imageView
只读获取尺寸
- (CGRect)backgroundRectForBounds:(CGRect)
bounds
- (CGRect)contentRectForBounds:(CGRect)
bounds
- (CGRect)titleRectForContentRect:(CGRect)
contentRect
- (CGRect)imageRectForContentRect:(CGRect)
contentRect
背景尺寸
内容尺寸
文本内容尺寸
图片尺寸
按钮样式
UIButtonTypeCustom和 UIButtonTypeRoundedRect 的区别
UIButtonTypeCustom:是一个背景透明的按钮。 如果想做按钮中图片切换(不同状态),必须使用两张图片进行设置。
UIButtonTypeRoundedRect:是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。
UIButtonTypeCustom:是一个背景透明的按钮。 如果想做按钮中图片切换(不同状态),必须使用两张图片进行设置。
UIButtonTypeRoundedRect:是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。
建议:快速开发,每个按钮只有一张图片,建议使用UIButtonTypeRoundedRect。按钮高度自定义,建议使用UIButtonTypeCustom。
按钮扩大触摸响应区域
原代码如下:menuBtn.frame = CGRectMake(4,8,44,28);
扩大触摸区域代码如下:
[menuBtn setImage:[PYUtiles imageFromFile:@“MenuBtn.png”] forState:UIControlStateNormal];
[menuBtn setFrame:CGRectMake(0,0,48,44)];
[menuBtn setContentMode:UIViewContentModeCenter];
原理,扩大button的frame rect,并且将图片设置成居中即可
[menuBtn setImage:[PYUtiles imageFromFile:@“MenuBtn.png”] forState:UIControlStateNormal];
[menuBtn setFrame:CGRectMake(0,0,48,44)];
[menuBtn setContentMode:UIViewContentModeCenter];
原理,扩大button的frame rect,并且将图片设置成居中即可
注意,这边要是setImage,setBackgroundImage不行,会扩大图片到整个响应区域
0 0
- UIButton
- UIButton
- UIButton
- uibutton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- UIButton
- UIBUTTON
- UIButton
- UIButton
- UIButton
- 逻辑运算符(And、AndAlso、Or、OrElse、&&、&、||、|)
- 线性判别分析(Linear Discriminant Analysis, LDA)算法分析
- Xcode终端爆炸之显示TEMPLATE END的解决办法
- 云服务器是什么 ?云服务器有什么优势 ?
- TCP/IP详解 卷1 笔记 第17章 TCP:传输控制协议
- UIButton
- linux 清空文件内容若干方法
- 机器视觉算法之物体方位特征提取
- 求逆序数
- UITextField
- C# ADO - timeout 引出的探讨
- 8086汇编初学者问题FAQ
- Android Context 上下文 你必须知道的一切
- UIControl