-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUIButton+MiddleAligning.m
More file actions
106 lines (80 loc) · 4.04 KB
/
Copy pathUIButton+MiddleAligning.m
File metadata and controls
106 lines (80 loc) · 4.04 KB
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
//
// UIButton+MiddleAligning.m
// UIButton+MiddleAligning
//
// Created by Barry on 12/11/15.
// Copyright © 2015 BarryLee. All rights reserved.
//
#import "UIButton+MiddleAligning.h"
@interface UIImage (MiddleAligning)
@end
@implementation UIImage (MiddleAligning)
- (UIImage *)MiddleAlignedButtonImageScaleToSize:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage);
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
@end
@implementation UIButton (MiddleAligning)
- (void)middleAlignButtonWithSpacing:(CGFloat)spacing {
UIUserInterfaceLayoutDirection direction = [UIApplication sharedApplication].userInterfaceLayoutDirection;
if ([self respondsToSelector:@selector(effectiveUserInterfaceLayoutDirection)]) {
direction = self.effectiveUserInterfaceLayoutDirection;
}
[self middleAlignButtonWithSpacing:spacing direction:direction];
}
- (void)middleAlignButtonWithSpacing:(CGFloat)spacing direction:(UIUserInterfaceLayoutDirection)direction {
UIControlState state = UIControlStateNormal;
NSString *titleString = [self titleForState:state]? : @"";
NSDictionary *attributes = @{NSFontAttributeName : self.titleLabel.font};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:titleString attributes:attributes];
CGSize titleSize = [attributedString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil].size;
CGSize imageSize = [self imageForState:state].size;
CGFloat maxImageHeight = CGRectGetHeight(self.frame) - titleSize.height - spacing * 2;
CGFloat maxImageWidth = CGRectGetWidth(self.frame);
UIImage *image = self.imageView.image;
UIImage *newImage = nil;
if (imageSize.width > ceilf(maxImageWidth)) {
CGFloat ratio = maxImageWidth / imageSize.width;
newImage = [image MiddleAlignedButtonImageScaleToSize:CGSizeMake(maxImageWidth, imageSize.height * ratio)];
imageSize = newImage.size;
}
if (imageSize.height > ceilf(maxImageHeight)) {
CGFloat ratio = maxImageHeight / imageSize.height;
newImage = [image MiddleAlignedButtonImageScaleToSize:CGSizeMake(imageSize.width * ratio, maxImageHeight)];
imageSize = newImage.size;
}
if (newImage != nil) {
if ([newImage respondsToSelector:@selector(imageWithRenderingMode:)]) {
newImage = [newImage imageWithRenderingMode:image.renderingMode];
}
[self setImage:newImage forState:state];
}
CGFloat imageVerticalDiff = titleSize.height + spacing;
CGFloat imageHorizontalDiff = titleSize.width;
CGFloat titleVerticalDiff = imageSize.height + spacing;
CGFloat titleHorizontalDiff = imageSize.width;
UIEdgeInsets imageEdgeInsets, titleEdgeInsets;
switch (direction) {
case UIUserInterfaceLayoutDirectionLeftToRight:
imageEdgeInsets = UIEdgeInsetsMake(-imageVerticalDiff, 0, 0, -imageHorizontalDiff);
titleEdgeInsets = UIEdgeInsetsMake(0, -titleHorizontalDiff, -titleVerticalDiff, 0);
break;
case UIUserInterfaceLayoutDirectionRightToLeft:
imageEdgeInsets = UIEdgeInsetsMake(-imageVerticalDiff, -imageHorizontalDiff, 0, 0);
titleEdgeInsets = UIEdgeInsetsMake(0, 0, -titleVerticalDiff, -titleHorizontalDiff);
break;
}
self.imageEdgeInsets = imageEdgeInsets;
self.titleEdgeInsets = titleEdgeInsets;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
@end