LGAlertView 按钮定制进阶指南:图标、字体与3个动态禁用技巧
LGAlertView 按钮定制进阶指南:图标、字体与3个动态禁用技巧
LGAlertView 是一款可深度定制的 iOS 弹窗视图组件(AlertView / ActionSheet 的替代方案),本文聚焦它的按钮定制进阶玩法:按钮图标、字体缩放与动态禁用,帮你在几行代码内做出符合设计规范的弹窗按钮。
一、按钮图标:3种状态图与左右位置设置
LGAlertView 中每个按钮(普通按钮 / 取消按钮 / 危险按钮)都支持按状态提供图标,共 3 种:
| 属性 | 触发时机 | 说明 |
|---|---|---|
buttonsIconImages |
默认状态 | 数组,按按钮下标一一对应 |
buttonsIconImagesHighlighted |
手指按下时 | 建议配一组"高亮态"图片增强反馈 |
buttonsIconImagesDisabled |
按钮被禁用时 | 通常配一张灰色/降低透明度的图 |
取消按钮和危险按钮则用单数形式:cancelButtonIconImage、destructiveButtonIconImage(各自同样有 Highlighted / Disabled 变体),属性声明见 LGAlertView.h。
alertView.buttonsIconImages = @[[UIImage imageNamed:@"Button"]];
alertView.buttonsIconImagesHighlighted = @[[UIImage imageNamed:@"ButtonHighlighted"]];
alertView.buttonsTextAlignment = NSTextAlignmentLeft;
完整可运行的示例在 Demo 的 TableViewControllerAlert.m 中,对应的多倍率素材(25/50/100pt)放在 Images.xcassets/Button.imageset/。
图标位置由枚举控制,支持左边(默认)或右边,定义见 LGAlertViewShared.h:
alertView.buttonsIconPosition = LGAlertViewButtonIconPositionRight;
💡 小技巧:把按钮标题传空字符串 @<a href="https://link.gitcode.com/i/8ef8fab94c4a01781005b1f8f642f603" target="_blank">@""] 并配上图标,就能做出"纯图标按钮",Demo 的 [case 9 就是这么实现的。
Demo 项目里模糊弹窗的示例就使用了一张 1024×1024 的雪山图作为背景,按钮图标与文字会叠在毛玻璃弹窗上显示:
二、按钮字体与文字排版:5个参数一次讲透
按钮文字相关的全局属性集中在 LGAlertView.h,每个属性都有合理的默认值:
| 属性 | 默认值 | 作用 |
|---|---|---|
buttonsFont |
18pt 系统字体 | 按钮文字字体 |
buttonsNumberOfLines |
1 | 允许的最大行数,0 表示不限 |
buttonsLineBreakMode |
中间省略号 | 超出一行时的截断方式 |
buttonsMinimumScaleFactor |
14/18 | 字号可缩小的下限比例 |
buttonsAdjustsFontSizeToFitWidth |
YES | 长文字自动缩小字号以塞进按钮 |
alertView.buttonsFont = [UIFont boldSystemFontOfSize:16.0];
alertView.buttonsNumberOfLines = 0; // 长文案直接换行,不再截断
⚠️ 注意:由于默认开启了自动缩字号(adjustsFontSizeToFitWidth = YES),遇到超长的按钮文字会自动缩小到 minimumScaleFactor 的比例;如果希望换行而不是缩小,把 buttonsNumberOfLines 设为 0 即可,效果可见 Demo 的 case 11。
三、动态禁用按钮:2种实战姿势
姿势1:展示前整体禁用
先用 setButtonEnabled:NO atIndex: 禁用某个按钮,再调用 show,用户看到的就是灰态按钮:
[self.securityAlertView setButtonEnabled:NO atIndex:0]; // "Done"按钮先禁用
[self.securityAlertView showAnimated:YES completionHandler:nil];
典型场景是"输入框弹窗":账号密码没填完时,"完成"按钮保持禁用(Demo 的 case 15)。此外还有三个批量开关:buttonsEnabled、cancelButtonEnabled、destructiveButtonEnabled(默认均为 YES)。
姿势2:随用户输入实时启用
这才是"动态"禁用的精髓——在输入回调里判断条件,随时切换按钮状态,Demo 的实现见 TableViewControllerAlert.m:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
UITextField *second = self.securityAlertView.textFieldsArray[(textField.tag == 0 ? 1 : 0)];
NSMutableString *currentString = textField.text.mutableCopy;
[currentString replaceCharactersInRange:range withString:string];
// 两个输入框都超过2个字符时才启用"完成"按钮
[self.securityAlertView setButtonEnabled:(currentString.length > 2 && second.text.length > 2) atIndex:0];
return YES;
}
配合只读方法 isButtonEnabledAtIndex: 还可以在"回车提交"前二次校验,避免用户误提交。
四、单按钮定制利器:setButtonPropertiesAtIndex
全局属性是"一刀切",想单独美化某一个按钮,用 setButtonPropertiesAtIndex:handler: 按下标定制,方法声明见 LGAlertView.h:
[alertView setButtonPropertiesAtIndex:0 handler:^(LGAlertViewButtonProperties *properties) {
properties.titleColor = UIColor.yellowColor;
properties.font = [UIFont systemFontOfSize:16.0];
properties.iconImage = [UIImage imageNamed:@"Button"];
properties.iconPosition = LGAlertViewButtonIconPositionLeft;
properties.enabled = NO; // 顺便禁用
}];
LGAlertViewButtonProperties 完整开放了文字颜色、背景色、图标(各含 3 种状态)、字体、行数、截断模式、缩放、图标位置、启用状态等全部属性,属性清单见 LGAlertViewButtonProperties.h。
五、最佳实践与常见坑
- 🎯 属性只能改一次:
[LGAlertView appearance]这类外观属性在弹窗show之后就无法再修改;但setButtonEnabled:atIndex:是运行时方法,弹窗展示期间随时可调——这正是动态禁用的基础。 - 🎨 高亮图不要省:按下时的 Highlighted 图标 + 背景色是弹窗"手感"的关键,Demo 的 CustomAlert case 8 展示了背景色+高亮色组合的效果。
- 📏 图标素材给三档:按 Demo 的做法提供 1x/2x/3x(如 25/50/100pt),避免高分屏发虚。
- ✅ 统一风格走 appearance,个别按钮走 index:全局用
tintColor一次搞定,再对个别按钮做差异定制,代码最简洁。
关键文件速查
| 文件 | 内容 |
|---|---|
| LGAlertView.h | 全部按钮全局属性声明 |
| LGAlertViewButtonProperties.h | 单按钮定制属性类 |
| LGAlertViewShared.h | 图标位置枚举 |
| TableViewControllerAlert.m | 图标与动态禁用完整 Demo |
| TableViewControllerCustomAlert.m | 按钮配色风格示例 |
掌握以上图标三态、字体缩放与动态禁用三块拼图,LGAlertView 的按钮定制就已经够用 90% 的业务场景了,剩下的细节全部藏在 Demo 项目 里等你探索。
