iOS 自定义UIButton(图片和文字混合)
// UIApplicationDelegate .h文件
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
// UIApplicationDelegate .m文件
#import "AppDelegate.h"
#import "CustomButton.h"
@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];
CustomButton *customBtn = [[CustomButton alloc] init];
customBtn.frame = CGRectMake(100,150, 150, 60);
[customBtn setTitle:@"十二年" forState:0];
[customBtn setImage:[UIImage imageNamed:@"De.png"] forState:0];
[self.window addSubview:customBtn];
[self.window makeKeyAndVisible];
return YES;
}
@end
// 自定义CustomButton类,继承UIButton
// CustomButton .h文件
#import <UIKit/UIKit.h>
@interface CustomButton : UIButton
@end
// CustomButton .m文件
#import "CustomButton.h"
@implementation CustomButton
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//设置粗体文字,以及文字的大小
self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
// 设置文字居右
self.titleLabel.textAlignment = NSTextAlignmentRight;
// 设置文字的颜色
[self setTitleColor:[UIColor redColor] forState:0];
// 设置背景图片
self.backgroundColor = [UIColor greenColor];
}
return self;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGRect titleFrame = CGRectMake(0, 0, contentRect.size.width * 0.6, contentRect.size.height);
return titleFrame;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(contentRect.size.width * 0.6, 0, contentRect.size.width * 0.4, contentRect.size.height);
}
@end
iOS 自定义UIButton(图片和文字混合)的更多相关文章
- 自定义带图片和文字的ImageTextButton
今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学 ...
- Android TextView中有图片有文字混合排列
Android TextView中有图片有文字混合排列 1.使用html.fromHtml 2.新建ImageGetter 3.使用<img src>标签 demo: 1.设置文字 ((T ...
- 自定义带图片和文字的Button的排版--陈棚
自定义button,动态加载图片与文字 [footView addSubview:btnAllChoose]; [btnAllChoose setTitle:str forState:UIContro ...
- 改变UIButton 图片和文字的位置
//设置字体和图片之间的间距 _btnLeft.titleEdgeInsets = UIEdgeInsetsMake(0, -_btnLeft.imageView.frame.size.width, ...
- 自定义组件---图片和文字实现ImageButton效果
1.效果图 2.自定义代码: <span style="font-family:Comic Sans MS;font-size:14px;">public class ...
- iOS 自定义UIButton
工作中有一个点击button更新button上文案的需求,用自定义了button可以很简单的实现的这个需求 首先写个自定义的button CustomButton.h #import <UIKi ...
- h5 安卓/IOS长按图片、文字禁止选中或弹出系统菜单 的解决方法
最近在做IM的语音功能,发现当长按录音的时候手机会弹出来系统菜单, IOS下bug形式:1)长按的标签设置为css background的形式:不会弹出菜单: 2)但是当设置为img时,系统默认识别为 ...
- UIButton图片与文字位置调整
1:左图右文 默认效果就行 2:左文右图 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn addTarget:se ...
- ios自定义UIButton内部空间Rect
随机推荐
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- HTML: 用表格畫出課程表
這個知識點,常常被我忽略,而且誤解(其實不是我誤解),曾經一個公司的要求:不使用table,一律用div,即使是整齊的,能夠使用table輕鬆佈局出的樣式,也一定要用div. 可能這傢伙沒搞清楚,ta ...
- 20145317彭垚 《Java程序设计》第10周学习总结
20145317彭垚 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网 ...
- RSA
https://en.wikipedia.org/wiki/RSA_(cryptosystem)
- pointer
https://en.wikipedia.org/wiki/Pointer_(computer_programming) In computer science, a pointer is a pro ...
- Python文件方法
打开文件 使用open函数,语法格式为:open( name[, mode[, buffering]]),name为打开文件名,mode为打开文件方式,buffering控制文件的缓冲. mode可选 ...
- 从cookie的setDomain方法可以得知localhost不是域名
http://www.baidu.com 其中baidu.com是一个域名.那么http://localhost 中的localhost是不是域名呢?我百度过,发现有人说这是域名.于是我在自己的web ...
- 文件管理php代码操作文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- linux configure
Linux环境下的软件安装,并不是一件容易的事情;如果通过源代码编译后在安装,当然事情就更为复杂一些;现在安装各种软件的教程都非常普遍;但万变不离其中,对基础知识的扎实掌握,安装各种软件的问题就迎刃而 ...
- JS中注意原型链的“指向”
昨天压缩Js文件时发现了项目中的一个prototype的问题代码如下所示: 1. <script> var XXX = function(){ }; var x1 = new XXX(); ...