代码自定义双色title的按钮
来源:互联网 发布:seo日志分析工具 编辑:程序博客网 时间:2024/05/01 00:20
所图所示,通过代码自定义这样的按钮。
.h文件
//// CustomButtom.h// testPlus//// Created by 鹰眼 on 14/10/20.// Copyright (c) 2014年 QCloud. All rights reserved.//#import <UIKit/UIKit.h>#define KDefaultColor [UIColor colorWithRed:66/255.0 green:189/255.0 blue:146/255.0 alpha:1.0]@interface CustomButtom : UIButton-(void)setNumber:(NSString *)number andTitle:(NSString *)title;@end
.m文件
//// CustomButtom.m// testPlus//// Created by 鹰眼 on 14/10/20.// Copyright (c) 2014年 QCloud. All rights reserved.//#import "CustomButtom.h"@implementation CustomButtom-(instancetype)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { self.backgroundColor=[UIColor groupTableViewBackgroundColor]; self.titleLabel.numberOfLines=0; self.titleLabel.textAlignment=NSTextAlignmentCenter; self.titleLabel.font=[UIFont systemFontOfSize:16.0]; self.backgroundColor=[UIColor colorWithRed:53/255.0 green:65/255.0 blue:72/255.0 alpha:1.0]; } return self; }-(void)setNumber:(NSString *)number andTitle:(NSString *)title{ [self setAttributedTitle:[self content:[NSString stringWithFormat:@"%@\n%@",number,title] colorString:number] forState:UIControlStateNormal];}-(NSAttributedString *)content:(NSString *)content colorString:(NSString *)search{ NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:content]; [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:[UIColor whiteColor] range:[content rangeOfString:content]]; [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:KDefaultColor range:[content rangeOfString:search]]; return attString;}@end
通过这样就可以自定义拥有两个颜色的按钮了,完全可以不用图片实现。
0 0
- 代码自定义双色title的按钮
- 自定义的Title
- AlertDialog的title自定义
- 自定义的Title(VBS版本)
- Android自定义Title的实现
- android自定义title的方法
- 带自定义title的tabhost
- TabLayout自定义Tab的title
- 动态变化的自定义Title
- 设置UINavigationBar的title, 左右按钮等
- 修改UINavigationController中leftBarButtonItem按钮的title
- 设置UINavigationBar的title, 左右按钮等
- 修改UINavigationController中leftBarButtonItem按钮的title
- 自定义Title
- 自定义Title
- 自定义title
- 自定义title
- 自定义title
- Android开发之SQLite的使用方法
- POJ 2186 Popular Cows(强连通)
- ios7禁止默认划动返回
- java学习笔记32
- u-boot-2014.10移植第1天----熟悉u-boot(一)
- 代码自定义双色title的按钮
- linux下面用C语言设计实现一个计算器
- 增加桶式的动态哈希(二)
- 基于c++的图像的灰度修正
- iOS中NSAttributedString的使用--对关键字着色,以及处理html实例
- IOS程序名称及内容国际化(本地化)
- VS2010 尝试运行项目时出错:无法启动调试
- [LeetCode]Merge Two Sorted Lists
- mongodb的启动参数详细说明