// 实例化 button 对象UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];// 将 button 添加到 window[self.view addSubview:button];
// 设置 button 类型/*UIButtonTypeCustom = 0, // 定制按钮,不带图标(常加载图片),默认文字颜色为白色,无触摸时的高亮效果UIButtonTypeSystem // 不带图标,默认文字颜色为蓝色,有触摸时的高亮效果UIButtonTypeDetailDisclosure, // “!” 图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果UIButtonTypeInfoLight, // “!” 图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果UIButtonTypeInfoDark, // “!” 图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果UIButtonTypeContactAdd, // “+” 图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果UIButtonTypeRoundedRect = UIButtonTypeSystem,[[UIButton alloc] init] 方式创建的 button 为 UIButtonTypeCustom 类型。StoryBoard 创建的 button 默认为 UIButtonTypeSystem 类型。*/UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];// 设置 framebutton.frame = CGRectMake(10, 100, 110, 50);// 设置内边距大小/*设置的内边距范围内不会显示内容*/// 内容内边距(标题文字和图片)button.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 10, 10);// 标题文字内边距button.titleEdgeInsets = UIEdgeInsetsMake(40, 20, 10, 10);// 图片内边距button.imageEdgeInsets = UIEdgeInsetsMake(20, 10, 10, 10);// 背景颜色/*默认为透明色(clearColor)*/button.backgroundColor = [UIColor grayColor];// 设置按钮的文字/*UIControlStateNormal = 0, // 未选中状态,常规状态UIControlStateHighlighted = 1 << 0, // 高亮状态,点击状态UIControlStateDisabled = 1 << 1, // 禁用状态UIControlStateSelected = 1 << 2, // flag usable by app (see below),选中状态UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focusUIControlStateApplication = 0x00FF0000, // additional flags available for application useUIControlStateReserved = 0xFF000000 // flags reserved for internal framework use*/[button setTitle:@"普通状态" forState:UIControlStateNormal];// 设置文字颜色[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// 设置文字阴影颜色[button setTitleShadowColor:[UIColor yellowColor] forState:UIControlStateNormal];// 修改文字字体button.titleLabel.font = [UIFont systemFontOfSize:30]; // 普通button.titleLabel.font = [UIFont boldSystemFontOfSize:30]; // 加粗button.titleLabel.font = [UIFont italicSystemFontOfSize:30]; // 斜体(对中文无效)button.titleLabel.font = [UIFont fontWithName:@"Zapfino" size:15]; // 设置为指定字体类型的文字// 修改文字水平对齐方式/*UIControlContentHorizontalAlignmentCenter = 0, // 居中UIControlContentHorizontalAlignmentLeft = 1, // 左对齐UIControlContentHorizontalAlignmentRight = 2, // 右对齐UIControlContentHorizontalAlignmentFill = 3, // 分散对齐*/button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;// 修改文字垂直对齐方式/*UIControlContentVerticalAlignmentCenter = 0, // 居中UIControlContentVerticalAlignmentTop = 1, // 上对齐UIControlContentVerticalAlignmentBottom = 2, // 下对齐UIControlContentVerticalAlignmentFill = 3, // 分散对齐*/button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;// 设置内部的小图片/*图片将会以原始比例显示,button 上的图片和文字可以同时添加*/[button setImage:[UIImage imageNamed:@"hehe.png"] forState:UIControlStateNormal];// 设置背景图片/*图片将会填充整个背景为了保证高亮状态下的图片正常显示,必须设置按钮的 type 为 custom*/[button setBackgroundImage:[UIImage imageNamed:@"pic1.png"] forState:UIControlStateNormal];// 设置图片不变暗/*在未设置触摸状态图片时,触摸状态或禁用状态下图片会变暗*/// 触摸状态时图标不变暗button.adjustsImageWhenHighlighted = NO;// 禁用状态时图标不变暗button.adjustsImageWhenDisabled = YES;// 设置 button 的激活与失效状态/*YES 激活状态,默认状态,NO 失效状态(不可点击)*/button.enabled = YES;// 设置 butotn 的隐藏与显示状态/*YES 隐藏状态,NO 显示状态,默认状态*/button.hidden = YES;// 设置 butotn 的选中状态/*YES 选中,NO 未选中*/button.selected = YES;// 获取 butotn 当前的选中状态BOOL selected = button.selected;// readonly 只读属性NSString *title = button.currentTitle; // 获取按钮当前的标题NSAttributedString *attributedTitle = button.currentAttributedTitle; // 获取按钮当前的标题属性UIColor *titleColor = button.currentTitleColor; // 获取按钮当前的标题颜色UIColor *shadowColor = button.currentTitleShadowColor; // 获取按钮当前的标题阴影颜色UIImage *currentImage = button.currentImage; // 获取按钮当前的图片UIImage *backgroundImage = button.currentBackgroundImage; // 获取按钮当前的背景图片NSString *title1 = [button titleForState:UIControlStateNormal]; // 获取按钮指定状态的文字// 获取按钮指定状态的标题属性NSAttributedString *attributedTitle1 = [button attributedTitleForState:UIControlStateNormal];// 获取按钮指定状态的标题颜色UIColor *titleColor1 = [button titleColorForState:UIControlStateNormal];// 获取按钮指定状态的标题阴影颜色UIColor *shadowColor1 = [button titleShadowColorForState:UIControlStateNormal];// 获取按钮指定状态的图片UIImage *currentImage1 = [button imageForState:UIControlStateNormal];// 获取按钮指定状态的背景图片UIImage *backgroundImage1 = [button backgroundImageForState:UIControlStateNormal];// 设置 button 点击触发事件/*UIControlEventTouchDown = 1 << 0, // on all touch downsUIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)UIControlEventTouchDragInside = 1 << 2,UIControlEventTouchDragOutside = 1 << 3,UIControlEventTouchDragEnter = 1 << 4,UIControlEventTouchDragExit = 1 << 5,UIControlEventTouchUpInside = 1 << 6,UIControlEventTouchUpOutside = 1 << 7,UIControlEventTouchCancel = 1 << 8,UIControlEventValueChanged = 1 << 12, // sliders, etc.UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.UIControlEventEditingDidBegin = 1 << 16, // UITextFieldUIControlEventEditingChanged = 1 << 17,UIControlEventEditingDidEnd = 1 << 18,UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editingUIControlEventAllTouchEvents = 0x00000FFF, // for touch eventsUIControlEventAllEditingEvents = 0x000F0000, // for UITextFieldUIControlEventApplicationReserved = 0x0F000000, // range available for application useUIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework useUIControlEventAllEvents = 0xFFFFFFFF*/[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
3.1 特点:
- UIButton:
- 既能显示文字,又能显示图片(能显示 2 张图片,背景图片和内容图片)。
- 长按高亮的时候可以切换图片\文字。
- 直接通过 addTarget... 方法监听点击。
- UIImageView:
- 能显示图片,不能直接通过 addTarget... 方法监听点击。
- UILabel:
- 能显示文字,不能直接通过 addTarget... 方法监听点击。
3.2 选择:
- 仅仅是显示数据,不需要点击
- 建议选择 UIImageView、UILabel。
- 不仅显示数据,还需要监听点击
- 其实 UIImageView、UILabel 也可以通过手势识别器来监听。
- 选择 UIButton
- 因为 UIButton 有 highlighted 这种状态。
- 同时显示 2 张图片:背景图片、内容图片
4.1 不要直接拿出按钮内部的子控件,来修改文字、图片属性,按钮内部的子控件设置是有状态的。
self.imageView.image = [UIImage imageNamed:@"shop.icon"];self.titleLabel.text = shop.name;
self setImage:[UIImage imageNamed:shop.icon] forState:UIControlStateNormal];[self setTitle:shop.name forState:UIControlStateNormal];
4.2 子控件 frame 设置
self.imageView.frame = CGRectMake(0, 0, buttonW, imageH);self.titleLabel.frame = CGRectMake(0, imageH, buttonW, buttonH - imageH);// 设置按钮中 title 的尺寸- (CGRect)titleRectForContentRect:(CGRect)contentRect { return CGRectMake(0, 30, 70, 30);}// 设置按钮中 imageView 的尺寸- (CGRect)imageRectForContentRect:(CGRect)contentRect { // 计算 imageView 控件尺寸,contentRect 为按钮的尺寸 CGFloat W = 40; CGFloat H = 46; CGFloat X = (contentRect.size.width - W) * 0.5; CGFloat Y = 20; return CGRectMake(X, Y, W, H);}
// 设置按钮接收点击事件的区域- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGFloat btnW = self.bounds.size.width; CGFloat btnH = self.bounds.size.height; // 计算不接收点击事件的区域 CGFloat X = 0; CGFloat Y = btnH / 2; CGFloat W = btnW; CGFloat H = Y; CGRect rect = CGRectMake(X, Y, W, H); if (CGRectContainsPoint(rect, point)) { return nil; } else { return [super hitTest:point withEvent:event]; }}