-
Notifications
You must be signed in to change notification settings - Fork 4
/
ButtonMaker.m
129 lines (98 loc) · 5.14 KB
/
ButtonMaker.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// ButtonMaker.m
// EasyLayout
//
// Created by Ben Ford on 10/31/12.
// Copyright (c) 2012 Ben Ford. All rights reserved.
//
#import "ButtonMaker.h"
#import "UIImage+Ext.h"
#import "NSString+Ext.h"
@implementation ButtonMaker
+ (UIButton *)genericButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
UIButton *newButton = [ButtonMaker textButtonWithText:title
font:[UIFont systemFontOfSize:15.0f] color:[UIColor blueColor]
normalImageName:nil selectedImageName:nil];
[newButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return newButton;
}
+ (UIButton *)plainButtonWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setImage:normalImage forState:UIControlStateNormal];
if (selectedImage != nil)
[newButton setImage:selectedImage forState:UIControlStateNormal];
newButton.frame = CGRectMake(0.0f, 0.0f, normalImage.size.width, normalImage.size.height);
return newButton;
}
+ (UIButton *)textButtonWithText:(NSString *)text font:(UIFont *)font color:(UIColor *)color normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName
{
UIImage *normalImage = [UIImage imageNamed:normalImageName];
UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setTitleColor:color forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor colorWithWhite:0.8f alpha:1.0f] forState:UIControlStateHighlighted];
newButton.titleLabel.font = font;
// set images if they were specified
if (normalImage != nil)
[newButton setBackgroundImage:normalImage forState:UIControlStateNormal];
if (selectedImage != nil)
[newButton setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
// size button via text
[ButtonMaker setText:text forButton:newButton maxWidth:CGFLOAT_MAX];
// override size with image if it was supplied
if (normalImage != nil)
newButton.frame = CGRectMake(0.0f, 0.0f, normalImage.size.width, normalImage.size.height);
return newButton;
}
+ (UIButton *)plainButtonWithNormalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName
{
UIImage *normalImage = [UIImage imageNamed:normalImageName];
UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setImage:normalImage forState:UIControlStateNormal];
if (selectedImage != nil)
[newButton setImage:selectedImage forState:UIControlStateSelected];
newButton.frame = CGRectMake(0.0f, 0.0f, normalImage.size.width, normalImage.size.height);
return newButton;
}
+ (UIButton *)outerButtonWithNormalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName transparentOuterArea:(CGSize)outerArea
{
UIButton *newButton = [ButtonMaker plainButtonWithNormalImageName:normalImageName selectedImageName:selectedImageName];
newButton.frame = CGRectMake(0.0f, 0.0f, newButton.frame.size.width+outerArea.width, newButton.frame.size.height+outerArea.height);
return newButton;
}
+ (void)setText:(NSString *)text forButton:(UIButton *)button maxSize:(CGSize)maxSize
{
[self setText:text forButton:button linebreakMode:NSLineBreakByWordWrapping constrainedToSize:maxSize];
}
+ (void)setText:(NSString *)text forButton:(UIButton *)button maxWidth:(CGFloat)maxWidth
{
[self setText:text forButton:button linebreakMode:NSLineBreakByTruncatingTail constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
}
+ (void)setText:(NSString *)text forButton:(UIButton *)button linebreakMode:(NSLineBreakMode)linebreakMode constrainedToSize:(CGSize)constrainedToSize
{
// the normalized text is used to calculate on something if incoming text was blank
NSString *normalizedText = [NSString extContainsText:text] == YES ? text : @"MMM";
// NewHouse and Futura fonts have a bug so pad them in the end
normalizedText = [NSString stringWithFormat:@"%@ ", normalizedText];
CGSize textSize;
switch (linebreakMode) {
case NSLineBreakByWordWrapping:
case NSLineBreakByCharWrapping:
textSize = [normalizedText sizeWithFont:button.titleLabel.font constrainedToSize:constrainedToSize lineBreakMode:linebreakMode];
break;
case NSLineBreakByClipping:
case NSLineBreakByTruncatingHead:
case NSLineBreakByTruncatingMiddle:
case NSLineBreakByTruncatingTail:
textSize = [normalizedText sizeWithFont:button.titleLabel.font forWidth:constrainedToSize.width lineBreakMode:linebreakMode];
break;
}
// don't actually use the normalized text
[button setTitle:text forState:UIControlStateNormal];
button.titleLabel.lineBreakMode = linebreakMode;
button.frame = CGRectMake(button.frame.origin.x,button.frame.origin.y,textSize.width,textSize.height);
}
@end