057调整屏幕中按钮的边间距

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController
4 @property (strong, nonatomic) UIButton *btnTitleEdgeInsets;
5
6 @end

ViewController.m

 1 #import "ViewController.h"
 2
 3 @interface ViewController ()
 4 - (void)disableStateSwitch:(UISwitch *)sender;
 5 - (void)buttonDidPush;
 6 @end
 7
 8 @implementation ViewController
 9
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     self.view.backgroundColor = [UIColor whiteColor];
13     self.navigationItem.title = @"调整屏幕中按钮的边间距";
14     UISwitch *swtDisableState = [[UISwitch alloc] init];
15     swtDisableState.on = YES;
16     [swtDisableState addTarget:self action:@selector(disableStateSwitch:) forControlEvents:UIControlEventValueChanged];
17     UIBarButtonItem *barBtnDisableState = [[UIBarButtonItem alloc] initWithCustomView:swtDisableState];
18     [self setToolbarItems:@[barBtnDisableState] animated:YES];
19
20     _btnTitleEdgeInsets = [UIButton buttonWithType:UIButtonTypeCustom];
21     _btnTitleEdgeInsets.frame = CGRectMake(0, 0, 320, 60);
22     _btnTitleEdgeInsets.center = self.view.center;
23     _btnTitleEdgeInsets.backgroundColor = [UIColor whiteColor];
24     _btnTitleEdgeInsets.layer.masksToBounds = YES;
25     _btnTitleEdgeInsets.layer.cornerRadius = 8.0;
26     //设置按钮标题字体及阴影颜色、行数
27     _btnTitleEdgeInsets.titleLabel.font = [UIFont boldSystemFontOfSize:16];
28     _btnTitleEdgeInsets.titleLabel.shadowOffset = CGSizeMake(2.0, 2.0);
29     _btnTitleEdgeInsets.titleLabel.numberOfLines = 2;
30     //设置普通状态下的显示特征
31     [_btnTitleEdgeInsets setTitle:@"点我进行状态图片切换
32 并且进行边间距调整" forState:UIControlStateNormal];
33     [_btnTitleEdgeInsets setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
34     [_btnTitleEdgeInsets setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];
35
36     //添加用于按钮各状态下的图片
37     [_btnTitleEdgeInsets setImage:[UIImage imageNamed:@"DogNormal"] forState:UIControlStateNormal];
38     [_btnTitleEdgeInsets setImage:[UIImage imageNamed:@"DogHighlighted"] forState:UIControlStateHighlighted];
39     [_btnTitleEdgeInsets setImage:[UIImage imageNamed:@"DogDisabled"] forState:UIControlStateDisabled];
40     //设置普通状态下的背景图片
41     UIImage *imgStretchable = [[UIImage imageNamed:@"Frame"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
42     [_btnTitleEdgeInsets setBackgroundImage:imgStretchable forState:UIControlStateNormal];
43
44     //设置按钮的内部边间距
45     UIEdgeInsets edgeInsets;
46     edgeInsets.left = edgeInsets.right = edgeInsets.top = edgeInsets.bottom = 0;
47     _btnTitleEdgeInsets.titleEdgeInsets = edgeInsets;
48     [_btnTitleEdgeInsets addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControlEventTouchUpInside];
49
50     [self.view addSubview:_btnTitleEdgeInsets];
51 }
52
53 - (void)didReceiveMemoryWarning {
54     [super didReceiveMemoryWarning];
55     // Dispose of any resources that can be recreated.
56 }
57
58 - (void)viewWillAppear:(BOOL)animated {
59     [super viewWillAppear:animated];
60     [self.navigationController setNavigationBarHidden:NO animated:animated];
61     [self.navigationController setToolbarHidden:NO animated:animated];
62 }
63
64 - (void)disableStateSwitch:(UISwitch *)sender {
65     _btnTitleEdgeInsets.enabled = sender.on;
66 }
67
68 - (void)buttonDidPush {
69     UIEdgeInsets edgeInsets;
70     edgeInsets.left = -80.0;
71     edgeInsets.right = edgeInsets.top = edgeInsets.bottom = 0;
72     if (_btnTitleEdgeInsets.contentEdgeInsets.left == edgeInsets.left) {
73         edgeInsets.left = 60.0;
74     }
75     _btnTitleEdgeInsets.contentEdgeInsets = edgeInsets;
76
77     if (_btnTitleEdgeInsets.titleEdgeInsets.left == 0) {
78         edgeInsets.left = 30.0;
79         _btnTitleEdgeInsets.titleEdgeInsets = edgeInsets;
80     }
81 }
82
83 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3
 4 @interface AppDelegate ()
 5 @end
 6
 7 @implementation AppDelegate
 8
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     [_window addSubview:_navigationController.view];
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33
34 @end
时间: 2024-11-13 02:21:15

057调整屏幕中按钮的边间距的相关文章

iOS 自动布局 Auto Layout 入门 06 详情页面 (中) 按钮的布局

上一节我们完成了对歌手名称label的布局设置,这一节我们对最下方的三个按钮的布局进行配置. 首先选中三个按钮,设置Size to Fit Content,让这三个按钮拥有合适的大小: 为了方便查看按钮的大小,我们为按钮设置一个背景色并设置如下图所示的约束: 在预览窗口,旋转界面为横屏,可以看到三个按钮的布局可以接受: 如果三个按钮宽度一致,看起来会更好看一些, 接下来我们将这三个按钮设置为等宽的: 由于约束始终是两两之间的,所以我们会看到有个按钮有两个带等号的约束,这是正常的. 如果都是英文,

调整屏幕亮度,调整字体大小

在我们开发项目的过程中,常常会遇到这样的功能,比如调整亮度,或者调整当前屏幕的字体大小, 其实这样的功能很好实现, 具体代码如下: 一.声明成员变量 1 { 2 UIView *_view; 3 BOOL isHidden; 4 UISlider *_slider; 5 UISlider *_twoSlider; 6 float value; 7 UILabel *_label; 8 NSInteger fontSize; 9 } 二.创建所需要改变的字体,一个label或是其他的控件,随意了

[WinForm][DevExpress]自定义GridControl中按钮文字内容

最近项目开发中,使用到了GridControl的FindPanel,这样可以很好的对数据进行筛选,可是所展现的按钮文字是英文,如图: 那怎么定义两个按钮问题,以符合项目需求了?经过一番搜索发现利用GridLocalizer可以很好实现: 核心代码: public class BuilderGridLocalizer : GridLocalizer { Dictionary<GridStringId, string> CusLocalizedKeyValue = null; /// <su

如何将任意文件固定在 Win10 的开始屏幕中

虽然Wox和Launchy是我日常启动程序的主力方式,不过开始屏幕的图标方便归类,这是快速启动工具所不能提供的,因此我也会将最常用的程序在开始屏幕上分类固定. 最近需要将一个常用的批处理文件(*.bat)固定在开始菜单中.但是发现不论是*.bat文件本身还是这个批处理的快捷方式(*.lnk),右键菜单中都没有固定到“开始”屏幕这个选项.那么如何实现将这个批处理固定到开始屏幕中呢? 其实开始菜单(C:\Users\Username\AppData\Roaming\Microsoft\Windows

调整Win7中TCP/IP半开连接数限制

调整Win7中TCP/IP半开连接数限制 相信大家都有过这样的经历,普通的ADSL宽带下,打开下载工具下载资源时,再想浏览网页就会变得非常困难了,Windows7中也未能幸免. 究其原因,一方面是某些下载软件在下载时为了追求速度会不惜占用全部带宽,另一方面也是由于微软出于安全考虑,限制了系统中的TCP/IP半开连接数. 而去除限制的方法也很简单: * WIN R运行regedit * 找到 HKEY_LOCAL_MACHINESYSTEM\CurrentControlSet\Services\T

ListView item中按钮点击实现删除

ListView item中按钮点击实现删除 在处理ListView 中item的按钮点击事件首先需要在item的根布局中添加 android:descendantFocusability="blocksDescendants"属性,否则item的点击事件会把其中image button的点击事件屏蔽掉 item的布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android中按钮的点击事件的四种写法

如题,在Android中按钮的点击事件有四种写法,如下图. 界面为四个Button+一个TextView+一个ImageView activity_main布局文件如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="

复制/剪切/粘贴屏幕中的图片

使用touchesEnded:withEvent:自定义复制粘贴菜单,copy:复制屏幕中的图片,cut:剪切屏幕中的图片,paste:粘贴屏幕中的图片. 1 @implementation prjCopyAndPaste 2 3 -(void)viewDidLoad 4 { 5 [super viewDidLoad]; 6 } 7 8 -(void)viewWillAppear:(BOOL)animated 9 { 10 [super viewWillAppear:animated]; 11

098在屏幕中实现一个检索框效果

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController<UISearchBarDelegate> 4 @property (strong, nonatomic) UISearchBar *searchBar; 5 @property (strong, nonatomic) NSMutableArray *mArrDataSourceO