zoukankan      html  css  js  c++  java
  • 代码自定义双色title的按钮

    所图所示,通过代码自定义这样的按钮。

    .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:@"%@
    %@",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
    

     通过这样就可以自定义拥有两个颜色的按钮了,完全可以不用图片实现。

  • 相关阅读:
    0209利用innobackupex进行简单数据库的备份
    0208如何利用federated配置远程的数据库和本地数据相互交互
    0208MySQL5.7之Group Replication
    解决问题的方法
    0123简单配置LNMP
    0120Keeplived实现自动切换Mysql服务
    0116MySql主从复制监控
    大数据导入EXCEL
    OSI结构和TCP/IP模型
    ORA-12154 TNS无法解析指定的连接标识符
  • 原文地址:https://www.cnblogs.com/wupei/p/4037885.html
Copyright © 2011-2022 走看看