UIButton 分类,使用了新的方法来设置带有文本和 FontAwesome 图标的按钮。打开 App
-(void)setBackgroundColor:(UIColor*)color forUIControlState:(UIControlState)state;
+(UIButton*)buttonWithType:(UIButtonType)type text:(NSString*)text icon:(NSString*)icon textAttributes:(NSDictionary*)attributes andIconPosition:(IconPosition)position;
-(id)initWithFrame:(CGRect)frame text:(NSString*)text icon:(NSString*)icon textAttributes:(NSDictionary*)attributes andIconPosition:(IconPosition)position;
,其中您可以使用 NSDictionary 指定文本/图标属性(您可以在 Apple 文档 中找到更多信息)。此外,您可以通过 IconPosition 参数指定 UIButton 中 Icon 的位置(左或右)-(void)setTextAttributes:(NSDictionary*)attributes forUIControlState:(UIControlState)state;
backgroundColor--(void)setBackgroundColor:(UIColor*)color forUIControlState:(UIControlState)state;
iconPosition--(void)setIconPosition:(IconPosition)position;
buttonText--(void)setButtonText:(NSString*)text;
buttonIcon--(void)setButtonIcon:(NSString*)icon;
buttonRadius--(void)setRadius:(CGFloat)radius;
安装 PPiAwesomeButton 最简单的方法是使用 CocoaPods
1) 将 Pod 添加到 Podfile
pod 'PPiAwesomeButton'
pod 'FontAwesome+iOS', :git => '[email protected]:alexdrone/ios-fontawesome.git'
2) 搜索项目中 Pod 的更新 pod install
3) 将 awesome 字体添加到您的 Info.plist,设置 UIAppFonts
条目作为数组,并将 FontAwesome.ttf
添加到这个数组中。
以下是一个生成具有类似 Twitter 设计的 UIButton 的示例
UIButton *twitter1=[UIButton buttonWithType:UIButtonTypeCustom text:@"Twitter" icon:@"icon-twitter" textAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]} andIconPosition:IconPositionLeft];
[twitter1 setBackgroundColor:[UIColor colorWithRed:27.0f/255 green:178.0f/255 blue:233.0f/255 alpha:1.0] forUIControlState:UIControlStateNormal];
[twitter1 setBackgroundColor:[UIColor colorWithRed:60.0f/255 green:89.0f/255 blue:157.0f/255 alpha:1.0] forUIControlState:UIControlStateHighlighted];
twitter1.frame=CGRectMake(10, 10, 120, 44);
[twitter1 setRadius:5.0];
[self.view addSubview:twitter1];
以下是一个 Pinterest 按钮的示例
UIButton *pinterest2=[UIButton buttonWithType:UIButtonTypeCustom text:@"Pin it!" icon:@"icon-pinterest" textAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:32],NSForegroundColorAttributeName:[UIColor whiteColor]} andIconPosition:IconPositionLeft];
[pinterest2 setBackgroundColor:[UIColor colorWithRed:205.0f/255 green:35.0f/255 blue:44.0f/255 alpha:1.0] forUIControlState:UIControlStateNormal];
[pinterest2 setBackgroundColor:[UIColor colorWithRed:244.0f/255 green:61.0f/255 blue:91.0f/255 alpha:1.0] forUIControlState:UIControlStateHighlighted];
pinterest2.frame=CGRectMake(10, 270, 280, 50);
[pinterest2 setRadius:0.0];
[self.view addSubview:pinterest2];
并且还有 Facetime
UIButton *facetime1=[UIButton buttonWithType:UIButtonTypeCustom text:@"Facetime" icon:@"icon-facetime-video" textAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]} andIconPosition:IconPositionRight];
[facetime1 setBackgroundColor:[UIColor colorWithRed:40.0f/255 green:219.0f/255 blue:31.0f/255 alpha:1.0] forUIControlState:UIControlStateNormal];
facetime1.frame=CGRectMake(10, 160, 120, 44);
[facetime1 setRadius:22.0];
[self.view addSubview:facetime1];
如果您检测到图标和文本之间存在一些错位,我已经创建了一个名为 UIAwesomeButton 的新类(UIView 子类),它具有与 UIButton 相同的行为,但从零开始实现(并且没有元素之间错位)。以下是将其实例化到您项目中的示例
UIAwesomeButton *button4 = [[UIAwesomeButton alloc] initWithFrame:CGRectMake(10, 400, 280, 50) text:@"Test" icon:nil textAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor],@"IconFont":[UIFont fontWithName:@"fontawesome" size:40]} andIconPosition:IconPositionLeft];
[button4 setBackgroundColor:[UIColor colorWithRed:205.0f/255 green:35.0f/255 blue:44.0f/255 alpha:1.0] forUIControlState:UIControlStateNormal];
[button4 setBackgroundColor:[UIColor colorWithRed:244.0f/255 green:61.0f/255 blue:91.0f/255 alpha:1.0] forUIControlState:UIControlStateHighlighted];
[button4 setRadius:3.0];
[button4 setSeparation:10];
[button4 setTextAlignment:NSTextAlignmentLeft];
[button4 setActionBlock:^{
NSLog(@"Working!");
}];
[self.view addSubview:button4];
您可以应用到有属性字符串中的文本属性。
NSString *const NSFontAttributeName;
NSString *const NSParagraphStyleAttributeName;
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;
NSString *const NSLigatureAttributeName;
NSString *const NSKernAttributeName;
NSString *const NSStrikethroughStyleAttributeName;
NSString *const NSUnderlineStyleAttributeName;
NSString *const NSStrokeColorAttributeName;
NSString *const NSStrokeWidthAttributeName;
NSString *const NSShadowAttributeName;
NSString *const NSVerticalGlyphFormAttributeName;
您可以在这里找到Awesome图标列表。每个图标都有一个标识符,您需要在UIButton中使用这个标识符来添加图标到您的UIButton。