Skip to content

Commit

Permalink
UIButton+LXMImagePosition
Browse files Browse the repository at this point in the history
  • Loading branch information
runlin authored and runlin committed Mar 28, 2016
1 parent 0e3ca3c commit 91eeb43
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Categories/UIKit/UIButton/UIButton+LXMImagePosition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UIButton+LXMImagePosition.h
// Demo_ButtonImageTitleEdgeInsets
//
// Created by luxiaoming on 16/1/15.
// Copyright © 2016年 luxiaoming. All rights reserved.
//
//https://github.com/Phelthas/Demo_ButtonImageTitleEdgeInsets
// 用button的titleEdgeInsets和 imageEdgeInsets属性来实现button文字图片上下或者左右排列的
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, LXMImagePosition) {
LXMImagePositionLeft = 0, //图片在左,文字在右,默认
LXMImagePositionRight = 1, //图片在右,文字在左
LXMImagePositionTop = 2, //图片在上,文字在下
LXMImagePositionBottom = 3, //图片在下,文字在上
};

@interface UIButton (LXMImagePosition)

/**
* 利用UIButton的titleEdgeInsets和imageEdgeInsets来实现文字和图片的自由排列
* 注意:这个方法需要在设置图片和文字之后才可以调用,且button的大小要大于 图片大小+文字大小+spacing
*
* @param spacing 图片和文字的间隔
*/
- (void)setImagePosition:(LXMImagePosition)postion spacing:(CGFloat)spacing;

@end
54 changes: 54 additions & 0 deletions Categories/UIKit/UIButton/UIButton+LXMImagePosition.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// UIButton+LXMImagePosition.m
// Demo_ButtonImageTitleEdgeInsets
//
// Created by luxiaoming on 16/1/15.
// Copyright © 2016年 luxiaoming. All rights reserved.
//

#import "UIButton+LXMImagePosition.h"

@implementation UIButton (LXMImagePosition)

- (void)setImagePosition:(LXMImagePosition)postion spacing:(CGFloat)spacing {
CGFloat imageWith = self.imageView.image.size.width;
CGFloat imageHeight = self.imageView.image.size.height;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
#pragma clang diagnostic pop

CGFloat imageOffsetX = (imageWith + labelWidth) / 2 - imageWith / 2;//image中心移动的x距离
CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
CGFloat labelOffsetX = (imageWith + labelWidth / 2) - (imageWith + labelWidth) / 2;//label中心移动的x距离
CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离

switch (postion) {
case LXMImagePositionLeft:
self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
break;

case LXMImagePositionRight:
self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);
break;

case LXMImagePositionTop:
self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
break;

case LXMImagePositionBottom:
self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
break;

default:
break;
}

}

@end
6 changes: 6 additions & 0 deletions IOS-Categories.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@
A2FD5B5D1A52706F00555EA2 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = A2FD5B5C1A52706F00555EA2 /* libz.dylib */; };
A2FD5B601A52708900555EA2 /* NSURL+Param.m in Sources */ = {isa = PBXBuildFile; fileRef = A2FD5B5F1A52708900555EA2 /* NSURL+Param.m */; };
A2FD5B631A52718200555EA2 /* NSDate+Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A2FD5B621A52718200555EA2 /* NSDate+Utilities.m */; };
B869399D1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.m in Sources */ = {isa = PBXBuildFile; fileRef = B869399C1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.m */; };
BCB799F31AFDD95300C12525 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB799F21AFDD95300C12525 /* UIImage+GIF.m */; };
BCB799F61AFDDAA500C12525 /* UIButton+BackgroundColor.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB799F51AFDDAA500C12525 /* UIButton+BackgroundColor.m */; };
BCB799FC1AFDDE2300C12525 /* UIView+ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BCB799FB1AFDDE2300C12525 /* UIView+ViewController.m */; };
Expand Down Expand Up @@ -1041,6 +1042,8 @@
A2FD5B5F1A52708900555EA2 /* NSURL+Param.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+Param.m"; sourceTree = "<group>"; };
A2FD5B611A52718200555EA2 /* NSDate+Utilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "NSDate+Utilities.h"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
A2FD5B621A52718200555EA2 /* NSDate+Utilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = "NSDate+Utilities.m"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
B869399B1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+LXMImagePosition.h"; sourceTree = "<group>"; };
B869399C1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+LXMImagePosition.m"; sourceTree = "<group>"; };
BCB799F11AFDD95300C12525 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; };
BCB799F21AFDD95300C12525 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = "<group>"; };
BCB799F41AFDDAA500C12525 /* UIButton+BackgroundColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+BackgroundColor.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1611,6 +1614,8 @@
A280E5221A52339800E6ACA2 /* UIButton */ = {
isa = PBXGroup;
children = (
B869399B1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.h */,
B869399C1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.m */,
A205E4D51C28E815002A1E33 /* UIButton+MiddleAligning.h */,
A205E4D61C28E815002A1E33 /* UIButton+MiddleAligning.m */,
A2B995C81B46778500011613 /* UIButton+TouchAreaInsets.h */,
Expand Down Expand Up @@ -3189,6 +3194,7 @@
A28BE33A1A3E9FDC005C4AC6 /* UIView+Nib.m in Sources */,
A21F34D61B0F28A600D73A91 /* UILabel+AutoSize.m in Sources */,
A2958CB71B35794500D7AA0F /* UIView+Visuals.m in Sources */,
B869399D1CA8EEB500EE7A1A /* UIButton+LXMImagePosition.m in Sources */,
A28BE32B1A3E9FDC005C4AC6 /* UIImage+Alpha.m in Sources */,
A28BE3361A3E9FDC005C4AC6 /* UITableViewCell+NIB.m in Sources */,
A281FBAD1AC812A8009040DA /* NSStringDemoViewController.m in Sources */,
Expand Down

0 comments on commit 91eeb43

Please sign in to comment.