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 ...
随机推荐
- Redis Sentinel哨兵集群
Redis Sentinel(哨兵集群)是一种高可用的redis部署方案.在集群中的redis-master服务挂掉时,无需人为干预,即可通过哨兵集群的自我调整,实现redis服务的持续可用. 哨兵集 ...
- Wikipedia : OIT history
http://en.wikipedia.org/wiki/Order-independent_transparency Order-independent transparency From Wiki ...
- pox目录中的交换机mac地址学习模块 l2_multi源码
# Copyright 2012-2013 James McCauley # # Licensed under the Apache License, Version 2.0 (the "L ...
- Memcached 笔记与总结(5)Memcached 的普通哈希分布和一致性哈希分布
普通 Hash 分布算法的 PHP 实现 首先假设有 2 台服务器:127.0.0.1:11211 和 192.168.186.129:11211 当存储的 key 经过对 2 (2 台服务器)取模运 ...
- ThinkPHP 学习笔记 ( 三 ) 数据库操作之数据表模型和基础模型 ( Model )
//TP 恶补ing... 一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: publ ...
- PHP pear安装
PHP pear安装 Posted on 2012-07-06 10:19 bug yang 阅读(5787) 评论(0) 编辑 收藏 转自:http://wangye.org/blog/archiv ...
- Java语言基础相关问题
*动手动脑: 问题1: 仔细阅读示例: EnumTest.java,运行它,分析运行结果? 源代码: public class EnumTest { public static void main ...
- Ogre初入手:最简单的ogre程序骨架
本文内容主要参考于页面 http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Ogre+Wiki+Tutorial+Framework Ogre是一个非 ...
- MVC @functions
asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...
- Decoder with 3 Inputs and 2 3 = 8 Outputs
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION