UIButton 按键

来源:互联网 发布:linux怎么看硬盘空间 编辑:程序博客网 时间:2024/05/17 05:52
概述:
    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:是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。
建议:快速开发,每个按钮只有一张图片,建议使用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];
原理,扩大buttonframe rect,并且将图片设置成居中即可
注意,这边要是setImagesetBackgroundImage不行,会扩大图片到整个响应区域
0 0
原创粉丝点击