iOS 对UIButton的imageView和titleLabel进行重新布局

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h"
#import "LFCustomButton.h"
@interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor]; LFCustomButton *btn = [LFCustomButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(, , , );
//注意:在此不能使用[btn setBackgroundImage:[UIImage imageNamed:@"label_comment@3x"] forState:0],否则达不到想要的效果
[btn setImage:[UIImage imageNamed:@"label_comment@3x"] forState:];
[btn setTitle:@"哈哈" forState:];
[btn setTitleColor:[UIColor blackColor] forState:];
[self.view addSubview:btn]; } @end
#import <UIKit/UIKit.h> @interface LFCustomButton : UIButton @end
#import "LFCustomButton.h"
const double LFButtonScale = 0.7;
@implementation LFCustomButton
/**
* 在layoutSubviews中修改布局
*/
- (void)layoutSubviews{
[super layoutSubviews]; //计算宽高
float imgHeight = MIN(self.bounds.size.width, self.bounds.size.height*LFButtonScale);
float imgWidth = imgHeight;
//imageView的尺寸
self.imageView.frame = CGRectMake((self.bounds.size.width - imgWidth)/2.0, ,imgWidth, imgHeight);
//titleLabel的尺寸
self.titleLabel.frame = CGRectMake(, self.bounds.size.height*LFButtonScale, self.bounds.size.width, self.bounds.size.height*(-LFButtonScale));
self.titleLabel.textAlignment = NSTextAlignmentCenter;
} @end
iOS 对UIButton的imageView和titleLabel进行重新布局的更多相关文章
- iOS学习-UIButton的imageView和titleLabel
UIButton的imageView和titleLabel的位置设置通过setImageEdgeInsets和setTitleEdgeInsets来设置 参考:http://blog.csdn.net ...
- ios开发之--UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- 格而知之1:UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void) ...
- (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等
UIButton是一个常用控件,使用方法十分基本,但是有很多技巧常常不被注意,本文主要介绍UIButton的一些较高级技巧,用于实现图片和标签显示的美观性等. 开发时常常碰到按钮的下侧或者右侧有标题的 ...
- IOS UIScrollView + UIButton 实现segemet页面和顶部标签页水平滚动效果
很长一段时间没有写博客了,最近在学习iOS开发,看了不少的代码,自己用UIScrollView和UIButton实现了水平滚动的效果,有点类似于今日头条的主界面框架,效果如下: 代码如下: MyScr ...
- iOS 之UIButton左文右图
对于button,当添加了图片时,默认是左图右文的 '[self.pageViewsLB setImage:[UIImage imageNamed:@"read"] forStat ...
- iOS 疑难杂症 — — UIButton 点击卡顿/延迟
前言 一开始还以为代码写的有问题,点击事件里面有比较耗时卡主线程的代码,逐一删减代码发现并不是这么回事. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.c ...
- iOS 自定义UIButton(图片和文字混合)
// UIApplicationDelegate .h文件 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...
随机推荐
- Apache Spark源码走读之13 -- hiveql on spark实现详解
欢迎转载,转载请注明出处,徽沪一郎 概要 在新近发布的spark 1.0中新加了sql的模块,更为引人注意的是对hive中的hiveql也提供了良好的支持,作为一个源码分析控,了解一下spark是如何 ...
- Android通过网页打开App到指定页面并传递数据
首先在 Android Manifest 文件中注册 intent-filter <activity android:name=".MainActivity" android ...
- jquery选中将select下拉框中一项后赋值给text文本框
jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...
- XBox 开发者大会
今天参加了微软的Xbox开发者大会,虽然没我什么事情,不过还是有不少的收获,随便说说自己的一点感受吧. 先上几张图,附带妹子一个,不过手机不清楚哈,~~ 1 ID@XBOX开发者计划与独立游戏开发者 ...
- TVP5150摄像头
工作中看同事摄像头配置的时候有2种格式PAL.NTSC.如果摄像头的格式配置不对的话会出现重影.黑白没颜色.闪屏等等. TVP5150 PAL.NTSC配置.http://bbs.csdn.net/ ...
- ajax+jsp自动刷新
通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...
- java面试问道的
1.java可重入锁 2.Hashmap原理.说说hashMap是怎样实现的(这个之前看过,顺利回答上.还回答了多线程的问题出现的原因,面试官表示很惊讶的样.用hashmap实现hashset 3.
- MySQL安装图解教程
安装文件存放路径:不能有中文和空格! 校验 1 安装MySQL 2 校验MySQL 登录MySQL:mysql -uroot -p123 退出MySQL:exit | quit 查看数据库:show ...
- JavaSE的知识
一 SE的知识体系: java基础: 一基础语法 8个基本数据类型-->8个包装类 数据类型转换: 自动转换(从小到大) 强制转换(从大到小) 注意:int 和char 分支与判断: if(){ ...
- Javascript实现提示错误的信息直接出现在输入框后
可以在输入框后加个<span id="error"></span>,当验证错误时这样处理document.getElementById('error').i ...