UIKit之浅析UIButton

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

可能有时候,想让title不在中间或者靠某一边,现在就举个例子怎么把文字在button的底部。

代码:

 button.contentEdgeInsets = UIEdgeInsetsMake(100, 0, 0, 0);

然后看一下 视图是这个样子:

这里边用到

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 = 0,                         // 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
时间: 2024-10-10 22:01:45

UIKit之浅析UIButton的相关文章

UIKit框架之UIButton详解

UIKit框架是iPhone应用程序开发中最基本的框架,也是用得最多.最重要的框架,今天要和大家分享的就是UIKit中的UIButton相关知识,一起来看看吧. 1.实例化: 1.1.init方式: 1 UIButton *button = [[UIButton alloc] initWithFrame:rect]; 1.2.类方法方式: 1 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 其中按钮类型枚

UIkit框架之UIbutton的使用

1.UIbutton的继承关系:UIcontroller:UIview:UIresponder:NSObject: 2.添加按钮的步骤: (1)创建按钮的时候首先设置类型 (2)添加标题或者图片,设置大小来适合按钮内的内容 (3)为这个按钮链接一个或者多个动作方法 (4)在界面设置按钮的布局来控制它的大小,位置 (5)提供有用的信息和局部字符串 3.按钮的类型有: (1)UIButtonTypeCustom(没有按钮类型), (2) UIButtonTypeSystem(圆角矩形), (3)UI

UIKit 框架之UIButton

列举几个稍微难点的属性,其他的方法属性都好理解,可以参照UIButton.h // // ViewController.m // UIButton // // Created by City--Online on 15/5/19. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @property(nonatomic,s

UIKit学习之UIButton篇

1.应用场景,文字居左,图片居中 //设置图片间距,使其居右对齐 btn.imageEdgeInsets = UIEdgeInsetsMake(0,btn.size.width - 12 - btn.imageView.image.size.width, 0, 0); //文字居左对齐 btn.titleEdgeInsets = UIEdgeInsetsMake(0, -(btn.imageView.image.size.width), 0, 0); //设置文字居左且中心对齐 [btn set

UIButton 加载网络图片

以后就可以 用这个分类   UIButton轻松加载网络图片了, UIButton+WebCache.h #import <UIKit/UIKit.h> @interface UIButton (WebCache) - (void)xr_setButtonImageWithUrl:(NSString *)urlStr; @end UIButton+WebCache.m #import "UIButton+WebCache.h" @implementation UIButto

Swift重写UIButton的图片和标题的位置

import UIKit class ResetBtn: UIButton { let IMAGE_RATIO :CGFloat = 0.7 // 图片占整个按钮高度的比例 let TITLE_FONT:CGFloat = 13 // 设置按钮标题字体默认的大小 override init(frame: CGRect) { super.init(frame: frame) self.setImageAndTitle() } required init?(coder aDecoder: NSCod

UIButton上同时显示图片和文字的方法

参考:http://blog.csdn.net/qijianli/article/details/8152726 不过有个问题,就是我使用时不能改变文字的颜色,后来修改了一下方法,如下: 定义一个UIButton+Manager文件,在.h #import <UIKit/UIKit.h> @interface UIButton (UIButtonImageWithLable) - (void) setImage:(UIImage *)image withTitle:(NSString *)ti

点击按钮改变状态的颜色

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC" } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC"; min-height: 17.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #e82300 }

Objective-C类族和工厂模式

本文转载至 http://www.cocoachina.com/ios/20141124/10296.html 相信大家都了解GoF的<Design Patterns>中提到的23种设计模式,其中将常见的设计模式分为三大类:创建型模式.行为型模式.结构型模式.而在<Clean Code>中也提到建造酒店的例子,系统中对象的构建和使用应当分离开,那么应该怎么构建对象更加整洁和符合使用场景就很重要. 在iOS的系统类库中也有一种方式使得开发者不必关注类中具体的存储实现,但可以根据不同需