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 ...
随机推荐
- iPads和iPhones的Media Queries
iPad Media Queries 1.iPad Media Queries (所有版本,包括iPad mini) iPads从第一代到至今,总共有五代,也就是iPad1~iPad5,以及Mini ...
- ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决
原因是view文件下面有个.htaccess文件,里面写的是 Deny from all //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...
- Mybatis分页和Spring的集成
写了一个Mybatis分页控件,在这记录一下使用方式. 在Maven中加入依赖: ? 1 2 3 4 5 6 7 8 9 <dependencies> ... <depe ...
- 【翻译】Kinect v2程序设计(C++) Body 篇
Kinect SDK v2预览版的主要功能的使用介绍,基本上完成了.这次,是关于取得Body(人体姿势)方法的说明. 上一节,是使用Kinect SDK v2预览版从Kinect v2预览版取得B ...
- DNS:www.flickr.com
203.84.197.9 203.84.197.25 203.84.197.26 203.84.197.27
- 1 2 3 n
n(n+1)/2 连续自然数 1,2,3.....,n 队列 从中任意取出1至n个相加,可以表示的连续自然数队列中最大的自然数是多少 受"高斯求和--蛇头蛇尾脑图--长方形对角线脑图--苯环 ...
- Greedy_algorithm
https://en.wikipedia.org/wiki/Greedy_algorithm
- docker-compose bug
annot mount volume over existing file, file exists /var/lib/docker/aufs/mnt/0ac71fed1af802a4ecf4a93b ...
- php--求几个数中的最小值
<?phpecho min(2, 3, 1, 6, 7); // 1echo min(array(2, 4, 5)); // 2echo min(0, 'hello'); // 0ec ...
- angularJS自定义指令间的“沟通”
由此例子我们可以看出,angularJS使用指令时link的执行顺序<html> <head> <meta charset="utf-8"/> ...