第二篇 - UIButton
初始化
// 能够定义的button类型有以下6种,
// typedef enum {
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号按钮
// } UIButtonType;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
注意,在初始化的时候就给他一个 样式。不然就无法给他样式了。因为
@property(nonatomic,readonly) UIButtonType buttonType; 是只读的。
注意。不可以这样设置按钮的文字和图片等
btn.titleLabel.text = @"哈哈哈";
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0);
... ...
这些都是只读的。
调用set方法进行设置
@class UIImage, UIFont, UIColor, UIImageView, UILabel;
//
// 按钮样式
// 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, // Deprecated, use UIButtonTypeSystem instead
// };
//
// NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl <NSCoding>
//
// + (instancetype)buttonWithType:(UIButtonType)buttonType;
//
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
//这个属性设置button里内容的偏移量,包括title和image,可以用如下方法设置
//btn.contentEdgeInsets=UIEdgeInsetsMake(20, 20, 0, 0);
@property(nonatomic) UIEdgeInsets titleEdgeInsets; // default is UIEdgeInsetsZero
@property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance
//按钮高亮时,是否改变阴影效果
@property(nonatomic) UIEdgeInsets imageEdgeInsets; // default is UIEdgeInsetsZero
@property(nonatomic) BOOL adjustsImageWhenHighlighted; // default is YES. if YES, image is drawn darker when highlighted(pressed)
//设置图片的绘制是否高亮时变暗
@property(nonatomic) BOOL adjustsImageWhenDisabled; // default is YES. if YES, image is drawn lighter when disabled
//设置图片是否 绘制当按钮禁用时
@property(nonatomic) BOOL showsTouchWhenHighlighted __TVOS_PROHIBITED; // default is NO. if YES, show a simple feedback (currently a glow) while highlighted
//设置是否显示手指印在按钮高亮的时候
@property(null_resettable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0); // The tintColor is inherited through the superview hierarchy. See UIView for more information.
//这个属性会作用于标题和图片,但是如果你是自定义风格的按钮,这个属性将不起任何作用,它只作用于系统的
@property(nonatomic,readonly) UIButtonType buttonType;
//
// // you can set the image, title color, title shadow color, and background image to use for each state. you can specify data
// // for a combined state by using the flags added together. in general, you should specify a value for the normal state to be used
// // by other states which don't have a custom value set
//
// setter
// - (void)setTitle:(nullable NSString *)title forState:(UIControlState)state; // default is nil. title is assumed to be single line
// - (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white
// - (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
// - (void)setImage:(nullable UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states
// - (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil
// - (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line
//
//getter
// - (nullable NSString *)titleForState:(UIControlState)state; // these getters only take a single state value
// - (nullable UIColor *)titleColorForState:(UIControlState)state;
// - (nullable UIColor *)titleShadowColorForState:(UIControlState)state;
// - (nullable UIImage *)imageForState:(UIControlState)state;
// - (nullable UIImage *)backgroundImageForState:(UIControlState)state;
// - (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
//
// // these are the values that will be used for the current state. you can also use these for overrides. a heuristic will be used to
// // determine what image to choose based on the explict states set. For example, the 'normal' state value will be used for all states
// // that don't have their own image defined.
//
//和上面的一样
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle; // normal/highlighted/selected/disabled. can return nil
@property(nonatomic,readonly,strong) UIColor *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor; // normal/highlighted/selected/disabled.
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage; // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage; // normal/highlighted/selected/disabled. can return nil
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // normal/highlighted/selected/disabled. can return nil
//
//这两个参数需要注意,虽然他们是只读属性不能重新设置,但是我们可以设置label和imageView的相关属性
// // return title and image views. will always create them if necessary. always returns nil for system buttons
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0);
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0);
//
// // these return the rectangle for the background (assumes bounds), the content (image + title) and for the image and title separately. the content rect is calculated based
// // on the title and image size and padding and then adjusted based on the control content alignment. there are no draw methods since the contents
// // are rendered in separate subviews (UIImageView, UILabel)
//
//返回背景大小
// - (CGRect)backgroundRectForBounds:(CGRect)bounds;
//返回视图大小,包括标题和图片
// - (CGRect)contentRectForBounds:(CGRect)bounds;
//返回文字的rect
// - (CGRect)titleRectForContentRect:(CGRect)contentRect;
//返回img的rect
// - (CGRect)imageRectForContentRect:(CGRect)contentRect;
@end
//
@interface UIButton(UIButtonDeprecated)
//
@property(nonatomic,strong) UIFont *font NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
@property(nonatomic) NSLineBreakMode lineBreakMode NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
@property(nonatomic) CGSize titleShadowOffset NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
//
@end
//