iOS界面编程-UIButton
来源:互联网 发布:电脑钢琴软件 多键 编辑:程序博客网 时间:2024/06/02 01:06
一、介绍
按钮在界面编程中非常常见,从移动端到客户端到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;
- letbutton = 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) UIButtonTypebuttonType
@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];都是系统提供的方法和属性 ,可以去试试
- iOS界面编程-UIButton
- iOS UI界面之UIButton
- iOS UI界面之UIButton
- IOS界面开发之UIButton
- iOS编程-------UITextField UIButton UITextFieldDelegate
- IOS 界面开发UIButton事件监听
- iOS界面编程-UILabel
- iOS界面编程-UIImageView
- iOS界面编程-UIPickerView
- iOS界面编程-UIProgressView
- iOS界面编程-UIActivityIndicatorView
- iOS界面编程-UIWebView
- iOS界面编程-UIAlertView
- iOS界面编程-UITextField
- iOS界面编程-UIDatePicker
- iOS界面编程-UIPageControl
- iOS开发-UIButton
- IOS-UIButton
- Django学习——开发你的第一个Django应用2
- 在Android5.0系统上加载位于SD卡上的本地动态库
- hdu4770 暴力状压 Lights Against Dudely (2013 Asia Hangzhou Regional Contest problem A)
- Java加密解密技术系列之DES
- 第二章 Winsock编程接口(1)
- iOS界面编程-UIButton
- Codeforces Round #321 (Div. 2)B. Kefa and Company
- 黑马程序员———java 集合框架
- 第二阶段
- You must restart adb and Eclipse的问题解决
- 在Unity中添加Googole广告
- 黑马程序员--NSMutableDictionary的使用
- MVC中DropDownListFor赋初始选择值问题
- 第六周项目四数据结构实践——数制转换(栈)