@@ -14,62 +14,91 @@ @interface UIImage (MiddleAligning)
1414
1515@implementation UIImage (MiddleAligning)
1616
17- - (UIImage *)MiddleAlignedButtonImageScaleToSize : (CGSize)size
18- {
17+ - (UIImage *)MiddleAlignedButtonImageScaleToSize : (CGSize)size {
1918 UIGraphicsBeginImageContextWithOptions (size, NO , 0 );
20-
19+
2120 CGContextRef context = UIGraphicsGetCurrentContext ();
22- CGContextTranslateCTM (context, 0.0 , size.height );
23- CGContextScaleCTM (context, 1.0 , -1.0 );
24- CGContextDrawImage (context, CGRectMake (0 . 0f , 0 . 0f , size.width , size.height ), self.CGImage );
25-
21+ CGContextTranslateCTM (context, 0 , size.height );
22+ CGContextScaleCTM (context, 1 .0f , -1 .0f );
23+ CGContextDrawImage (context, CGRectMake (0 , 0 , size.width , size.height ), self.CGImage );
24+
2625 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext ();
27-
26+
2827 UIGraphicsEndImageContext ();
29-
28+
3029 return scaledImage;
3130}
3231
3332@end
3433
3534@implementation UIButton (MiddleAligning)
3635
37- - (void )middleAlignButtonWithSpacing : (CGFloat)spacing
38- {
39- NSString *titleString = [self titleForState: UIControlStateNormal]?:@" " ;
40- NSAttributedString *attributedString = [[NSAttributedString alloc ] initWithString: titleString attributes: @{NSFontAttributeName : self.titleLabel .font }];
41- CGSize titleSize = [attributedString boundingRectWithSize: CGSizeMake (CGFLOAT_MAX, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin context: nil ].size ;
42- CGSize imageSize = [self imageForState: UIControlStateNormal].size ;
36+ - (void )middleAlignButtonWithSpacing : (CGFloat)spacing {
37+ UIUserInterfaceLayoutDirection direction = [UIApplication sharedApplication ].userInterfaceLayoutDirection ;
38+ if ([self respondsToSelector: @selector (effectiveUserInterfaceLayoutDirection )]) {
39+ direction = self.effectiveUserInterfaceLayoutDirection ;
40+ }
41+ [self middleAlignButtonWithSpacing: spacing direction: direction];
42+ }
43+
44+ - (void )middleAlignButtonWithSpacing : (CGFloat)spacing direction : (UIUserInterfaceLayoutDirection)direction {
45+ UIControlState state = UIControlStateNormal;
46+
47+ NSString *titleString = [self titleForState: state]? : @" " ;
48+ NSDictionary *attributes = @{NSFontAttributeName : self.titleLabel .font };
49+ NSAttributedString *attributedString = [[NSAttributedString alloc ] initWithString: titleString attributes: attributes];
50+ CGSize titleSize = [attributedString boundingRectWithSize: CGSizeMake (CGFLOAT_MAX, CGFLOAT_MAX)
51+ options: NSStringDrawingUsesLineFragmentOrigin
52+ context: nil ].size ;
53+
54+ CGSize imageSize = [self imageForState: state].size ;
55+
4356 CGFloat maxImageHeight = CGRectGetHeight (self.frame ) - titleSize.height - spacing * 2 ;
4457 CGFloat maxImageWidth = CGRectGetWidth (self.frame );
58+
59+ UIImage *image = self.imageView .image ;
60+
4561 UIImage *newImage = nil ;
62+
4663 if (imageSize.width > ceilf (maxImageWidth)) {
4764 CGFloat ratio = maxImageWidth / imageSize.width ;
48- newImage = [self .imageView. image MiddleAlignedButtonImageScaleToSize: CGSizeMake (maxImageWidth, imageSize.height * ratio)];
65+ newImage = [image MiddleAlignedButtonImageScaleToSize: CGSizeMake (maxImageWidth, imageSize.height * ratio)];
4966 imageSize = newImage.size ;
5067 }
5168 if (imageSize.height > ceilf (maxImageHeight)) {
5269 CGFloat ratio = maxImageHeight / imageSize.height ;
53- newImage = [self .imageView. image MiddleAlignedButtonImageScaleToSize: CGSizeMake (imageSize.width * ratio, maxImageHeight)];
70+ newImage = [image MiddleAlignedButtonImageScaleToSize: CGSizeMake (imageSize.width * ratio, maxImageHeight)];
5471 imageSize = newImage.size ;
5572 }
56- if (newImage) {
73+ if (newImage != nil ) {
5774 if ([newImage respondsToSelector: @selector (imageWithRenderingMode: )]) {
58- newImage = [newImage imageWithRenderingMode: self .imageView. image.renderingMode];
75+ newImage = [newImage imageWithRenderingMode: image.renderingMode];
5976 }
60- [self setImage: newImage forState: UIControlStateNormal ];
77+ [self setImage: newImage forState: state ];
6178 }
62-
79+
6380 CGFloat imageVerticalDiff = titleSize.height + spacing;
6481 CGFloat imageHorizontalDiff = titleSize.width ;
65-
66- self.imageEdgeInsets = UIEdgeInsetsMake (-imageVerticalDiff, 0 , 0 , -imageHorizontalDiff);
67-
82+
6883 CGFloat titleVerticalDiff = imageSize.height + spacing;
6984 CGFloat titleHorizontalDiff = imageSize.width ;
70-
71- self.titleEdgeInsets = UIEdgeInsetsMake (0 , -titleHorizontalDiff, -titleVerticalDiff, 0 );
72-
85+
86+ UIEdgeInsets imageEdgeInsets, titleEdgeInsets;
87+
88+ switch (direction) {
89+ case UIUserInterfaceLayoutDirectionLeftToRight:
90+ imageEdgeInsets = UIEdgeInsetsMake (-imageVerticalDiff, 0 , 0 , -imageHorizontalDiff);
91+ titleEdgeInsets = UIEdgeInsetsMake (0 , -titleHorizontalDiff, -titleVerticalDiff, 0 );
92+ break ;
93+ case UIUserInterfaceLayoutDirectionRightToLeft:
94+ imageEdgeInsets = UIEdgeInsetsMake (-imageVerticalDiff, -imageHorizontalDiff, 0 , 0 );
95+ titleEdgeInsets = UIEdgeInsetsMake (0 , 0 , -titleVerticalDiff, -titleHorizontalDiff);
96+ break ;
97+ }
98+
99+ self.imageEdgeInsets = imageEdgeInsets;
100+ self.titleEdgeInsets = titleEdgeInsets;
101+
73102 self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
74103 self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
75104}
0 commit comments