Skip to content

Commit

Permalink
工具类-->按钮添加
Browse files Browse the repository at this point in the history
  • Loading branch information
apple authored and apple committed Apr 25, 2018
1 parent 34db2d3 commit a5601d8
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 2 deletions.
Binary file modified .DS_Store
Binary file not shown.
28 changes: 27 additions & 1 deletion BigShow1949.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
18C2D5531BAEDDE7006CD3B2 /* libstdc++.6.0.9.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 18C2D5521BAEDDE7006CD3B2 /* libstdc++.6.0.9.dylib */; };
18C760041B9AB24A0038B2EE /* lxh in Resources */ = {isa = PBXBuildFile; fileRef = 18C760021B9AB24A0038B2EE /* lxh */; };
18C760051B9AB24A0038B2EE /* default in Resources */ = {isa = PBXBuildFile; fileRef = 18C760031B9AB24A0038B2EE /* default */; };
18CCA1692090200400300966 /* placeholder30.png in Resources */ = {isa = PBXBuildFile; fileRef = 18CCA1652090200400300966 /* placeholder30.png */; };
18CCA16A2090200400300966 /* YFButtonCategoryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CCA1662090200400300966 /* YFButtonCategoryViewController.m */; };
18CCA16B2090200400300966 /* UIButton+TitlePosition.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CCA1672090200400300966 /* UIButton+TitlePosition.m */; };
18E523571F443EEF00514693 /* APRoundedButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E51D991F443EED00514693 /* APRoundedButton.m */; };
18E523581F443EEF00514693 /* YFAllRoundButtonVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18E51D9B1F443EED00514693 /* YFAllRoundButtonVC.m */; };
18E523591F443EEF00514693 /* YFAllRoundButtonVC.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 18E51D9C1F443EED00514693 /* YFAllRoundButtonVC.storyboard */; };
Expand Down Expand Up @@ -755,6 +758,11 @@
18C2D5521BAEDDE7006CD3B2 /* libstdc++.6.0.9.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libstdc++.6.0.9.dylib"; path = "usr/lib/libstdc++.6.0.9.dylib"; sourceTree = SDKROOT; };
18C760021B9AB24A0038B2EE /* lxh */ = {isa = PBXFileReference; lastKnownFileType = folder; path = lxh; sourceTree = "<group>"; };
18C760031B9AB24A0038B2EE /* default */ = {isa = PBXFileReference; lastKnownFileType = folder; path = default; sourceTree = "<group>"; };
18CCA1642090200400300966 /* UIButton+TitlePosition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+TitlePosition.h"; sourceTree = "<group>"; };
18CCA1652090200400300966 /* placeholder30.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = placeholder30.png; sourceTree = "<group>"; };
18CCA1662090200400300966 /* YFButtonCategoryViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YFButtonCategoryViewController.m; sourceTree = "<group>"; };
18CCA1672090200400300966 /* UIButton+TitlePosition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+TitlePosition.m"; sourceTree = "<group>"; };
18CCA1682090200400300966 /* YFButtonCategoryViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFButtonCategoryViewController.h; sourceTree = "<group>"; };
18E51D981F443EED00514693 /* APRoundedButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APRoundedButton.h; sourceTree = "<group>"; };
18E51D991F443EED00514693 /* APRoundedButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APRoundedButton.m; sourceTree = "<group>"; };
18E51D9A1F443EED00514693 /* YFAllRoundButtonVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YFAllRoundButtonVC.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2099,6 +2107,18 @@
name = Frameworks;
sourceTree = "<group>";
};
18CCA1632090200400300966 /* UIButton */ = {
isa = PBXGroup;
children = (
18CCA1642090200400300966 /* UIButton+TitlePosition.h */,
18CCA1652090200400300966 /* placeholder30.png */,
18CCA1662090200400300966 /* YFButtonCategoryViewController.m */,
18CCA1672090200400300966 /* UIButton+TitlePosition.m */,
18CCA1682090200400300966 /* YFButtonCategoryViewController.h */,
);
path = UIButton;
sourceTree = "<group>";
};
18E51D951F443EED00514693 /* Classes */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4744,6 +4764,7 @@
18E522251F443EEE00514693 /* 11 - Tools(常用工具) */ = {
isa = PBXGroup;
children = (
18CCA1632090200400300966 /* UIButton */,
18E522261F443EEE00514693 /* Color */,
18E5222C1F443EEE00514693 /* EmptyDataSet */,
18E522381F443EEE00514693 /* Image */,
Expand Down Expand Up @@ -5424,7 +5445,7 @@
TargetAttributes = {
8B0B9AAE1B9572000084590D = {
CreatedOnToolsVersion = 6.4;
DevelopmentTeam = 686TP5D9Q7;
DevelopmentTeam = 3JJP29A3NU;
LastSwiftMigration = 0800;
};
8B0B9AC71B9572000084590D = {
Expand Down Expand Up @@ -5535,6 +5556,7 @@
18E5238E1F443EEF00514693 /* james_harden.png in Resources */,
18E524801F443EF000514693 /* leaves.png in Resources */,
18E524961F443EF000514693 /* Shader.vsh in Resources */,
18CCA1692090200400300966 /* placeholder30.png in Resources */,
18E5249B1F443EF000514693 /* Shader.fsh in Resources */,
18C760041B9AB24A0038B2EE /* lxh in Resources */,
18E524EB1F443EF100514693 /* 8.jpg in Resources */,
Expand Down Expand Up @@ -5683,6 +5705,7 @@
18E5237C1F443EEF00514693 /* YFShineLabelViewController.m in Sources */,
18E5241C1F443EEF00514693 /* Case8ViewController.m in Sources */,
18E524F71F443EF100514693 /* UINTAppDelegate.m in Sources */,
18CCA16A2090200400300966 /* YFButtonCategoryViewController.m in Sources */,
18E524C91F443EF000514693 /* SYHistoryViewController.m in Sources */,
18E525401F443EF100514693 /* Reachability.m in Sources */,
1868EEA62066419B00EEE713 /* JS_UIWebViewJSBridgeViewController.m in Sources */,
Expand Down Expand Up @@ -6158,6 +6181,7 @@
18E524EF1F443EF100514693 /* RACollectionViewTripletLayout.m in Sources */,
18E523941F443EEF00514693 /* YFHalfCircleLayoutViewController.m in Sources */,
18E525011F443EF100514693 /* YFCollisionBehaviorViewController.m in Sources */,
18CCA16B2090200400300966 /* UIButton+TitlePosition.m in Sources */,
18E5236C1F443EEF00514693 /* LazyFadeInLayer.m in Sources */,
18E523831F443EEF00514693 /* YFTagsCloudViewController.m in Sources */,
18E525041F443EF100514693 /* MenuData.m in Sources */,
Expand Down Expand Up @@ -6328,6 +6352,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 3JJP29A3NU;
ENABLE_BITCODE = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -6361,6 +6386,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 3JJP29A3NU;
ENABLE_BITCODE = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
8 changes: 8 additions & 0 deletions BigShow1949.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UIButton+TitlePosition.h
// test
//
// Created by apple on 2018/4/4.
// Copyright © 2018年 BigShow. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,YFTitleStyle) {
YFTitleStyleTitleOnly = 1, //只显示文字
YFTitleStyleImgOnly, //只显示图片
YFTitleStyleLeft, //文字在左,图片在右
YFTitleStyleRight, //文字在右,图片在左
YFTitleStyleTop, //文字在上,图片在下
YFTitleStyleBottom //文字在下,图片在上
};

@interface UIButton (TitlePosition)

//调用这个方法前,必须先设置好button的image和title/attributedtitle 要不然无法生效
- (void)layoutTitleWithStyle:(YFTitleStyle)style imageTitleSpace:(CGFloat)space;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// UIButton+TitlePosition.m
// test
//
// Created by apple on 2018/4/4.
// Copyright © 2018年 BigShow. All rights reserved.
//

#import "UIButton+TitlePosition.h"

@implementation UIButton (TitlePosition)

- (void)layoutTitleWithStyle:(YFTitleStyle)style imageTitleSpace:(CGFloat)space {
// self.backgroundColor = [UIColor cyanColor];

/**
* 前置知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
* 如果只有title,那它上下左右都是相对于button的,image也是一样;
* 如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
*/

// 1. 得到imageView和titleLabel的宽、高
CGFloat imageWith = self.imageView.frame.size.width;
CGFloat imageHeight = self.imageView.frame.size.height;

CGFloat labelWidth = 0.0;
CGFloat labelHeight = 0.0;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// 由于iOS8中titleLabel的size为0,用下面的这种设置
labelWidth = self.titleLabel.intrinsicContentSize.width;
labelHeight = self.titleLabel.intrinsicContentSize.height;
} else {
labelWidth = self.titleLabel.frame.size.width;
labelHeight = self.titleLabel.frame.size.height;
}

// 2. 声明全局的imageEdgeInsets和labelEdgeInsets
UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;

// 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch (style) {
case YFTitleStyleBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
}
break;
case YFTitleStyleRight:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
}
break;
case YFTitleStyleTop:
{
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
}
break;
case YFTitleStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
}
break;

default:
break;
}

// 4. 赋值
self.titleEdgeInsets = labelEdgeInsets;
self.imageEdgeInsets = imageEdgeInsets;
}
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// YFButtonCategoryViewController.h
// BigShow1949
//
// Created by apple on 2018/4/4.
// Copyright © 2018年 BigShowCompany. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YFButtonCategoryViewController : UIViewController

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// YFButtonCategoryViewController.m
// BigShow1949
//
// Created by apple on 2018/4/4.
// Copyright © 2018年 BigShowCompany. All rights reserved.
//

#import "YFButtonCategoryViewController.h"
#import "UIButton+TitlePosition.h"

@interface YFButtonCategoryViewController ()

@end

@implementation YFButtonCategoryViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
{

UIButton *btn = [[UIButton alloc] init];
btn.frame = CGRectMake(20, 100, 100, 100);
[btn setTitle:@"title" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"placeholder30"] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:btn];
[btn layoutTitleWithStyle:YFTitleStyleBottom imageTitleSpace:10];
}
{

UIButton *btn = [[UIButton alloc] init];
btn.frame = CGRectMake(150, 100, 100, 100);
[btn setTitle:@"title" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"placeholder30"] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:btn];
[btn layoutTitleWithStyle:YFTitleStyleTop imageTitleSpace:10];
}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ - (void)viewDidLoad {

[self setupDataArr:@[@[@"颜色转换",@"YFColorViewController_UIStoryboard"],
@[@"图片",@"YFImageCategoryViewController"],
@[@"无数据界面提示",@"YFEmptyDataSetViewController_UIStoryboard"],]];
@[@"无数据界面提示",@"YFEmptyDataSetViewController_UIStoryboard"],
@[@"按钮",@"YFButtonCategoryViewController"],]];

}

Expand Down

0 comments on commit a5601d8

Please sign in to comment.