IOS自定义控件

来源:互联网 发布:java采购管理系统 编辑:程序博客网 时间:2024/06/05 07:36

前段时间,公司在开发软件时。为了节省开发成本,就把美工切图这一块去掉了。为此~~,个人对这一块是非常的头疼。但不幸中的万幸是,从中学到点东西。首先就是矢量图标。有兴趣的可以看看阿里的:http://www.iconfont.cn。原理就是把项目中要用到的图片转化为特殊字体。在项目中以label的形式展示出来。听到这里是不是感觉还不错。

要想用iconfont需要配置环境  可以参照阿里的配置文档:http://www.iconfont.cn/help/iconuse.html

1、将您IconFont刚下载的字体文件(.ttf)添加到工程中;

2、打开Info.plist文件,增加一个新的Array类型的键,键名设置为UIAppFonts(Fonts provided by application),增加字体的文件名:“iconfont.ttf“


使用IconFont字体:

UILabel * label = [[UILabel alloc] initWithFrame:self.view.bounds];

UIFont *iconfont = [UIFont fontWithName:@"uxIconFont" size: 34];//这里需要注意下,@“”里面的类型需要与倒入工程ttf的类型一致。

label.font = iconfont;

label.text = @"\U00003439 \U000035ad \U000035ae \U000035af \U000035eb \U000035ec";

[self.view addSubview: label];

但是在工程中使用时又有点缺憾,就是当我为的按钮需要用到icon font时回很麻烦,无奈只能用自定义控件。

自定义button的.h文件

@property(nonatomic,strong)UILabel * IconLabel;

@property(nonatomic,assign)float PX;

.m中的实现

-(void)drawRect:(CGRect)rect

{

    CGRect rect1=[[UIScreen mainScreen] bounds];

    CGSize size=rect1.size;

    CGFloat WIDTH=size.width;

    UIFont *iconfont=nil;

    _IconLabel=[[UILabel alloc]initWithFrame:self.bounds];

    _IconLabel.textAlignment=NSTextAlignmentCenter;

    if (WIDTH==320)

    {

        iconfont = [UIFont fontWithName:@"IconFont" size: _PX];

    }

    else if (WIDTH==375)

    {

        iconfont = [UIFont fontWithName:@"IconFont" size: _PX+2];

    }

    else if (WIDTH==414)

    {

        iconfont = [UIFont fontWithName:@"IconFont" size: _PX+4];

    }

    self.IconLabel.font=iconfont;

    self.IconLabel.text=self.titleLabel.text;

     [self addSubview:self.IconLabel];

}

在调用的时候只需要倒入头文件实现就行

        _OrderStatusBtn=[[KCButtonalloc]initWithFrame:CGRectMake(WIDTH1*230/320, MarginY, WIDTH1*80/320, CarHeight)];

        _OrderStatusBtn.backgroundColor=CustomColor(61,216,76, 1);

         _OrderStatusBtn.layer.borderColor =CustomColor(61,216,76, 1).CGColor;

         _OrderStatusBtn.layer.borderWidth =1;

        _OrderStatusBtn.layer.cornerRadius =6;

        _OrderStatusBtn.layer.masksToBounds =YES;

        _OrderStatusBtn.PX=12;

         _OrderStatusBtn.titleLabel.textColor=[UIColorwhiteColor];

        [selfaddSubview:_OrderStatusBtn];






0 0
原创粉丝点击