iOS常用控件封装-SZKLabel(基于UILabel的封装)

对于UILabel的封装还是相对简单一些,把label的一些常用属性写到一个方法里面这样调用的时候直接就可以赋值进去了,先上调用代码

在ViewController.m导入#import "SZKLabel.h"
直接调用

- (void)viewDidLoad {
    [super viewDidLoad];
    SZKLabel *szkLabel=[SZKLabel labelWithFrame:CGRectMake(100, 100, 100, 100) text:@"SZKLabel" textColor:[UIColor redColor] font:[UIFont systemFontOfSize:16] textAlignment:NSTextAlignmentCenter backgroundColor:[UIColor grayColor]];
    [self.view addSubview:szkLabel];
}

两行代码搞定(其实一行代码也是可以的,只是如果把self.view也添加到上面的话,这样创建的变量就会有警告⚠️,强迫症伤不起)

上面只是封装了一些常用的属性,如果用不到的,赋值为nil,如果不够用,自己再添加相应属性,如果你的项目中可以用到可以直接拿走,用不到就当笔者在娱乐消遣了。

效果图:

SZKLabel效果图

实现方法:
在SZKLabel.h

#import <UIKit/UIKit.h>

@interface SZKLabel : UILabel

+(instancetype)labelWithFrame:(CGRect)frame
                         text:(NSString *)text
                    textColor:(UIColor *)textColor
                         font:(UIFont *)font
                textAlignment:(NSTextAlignment)textAlignment
              backgroundColor:(UIColor *)bgColor;
@end

在SZKLabel.m中

#import "SZKLabel.h"

@implementation SZKLabel

+(instancetype)labelWithFrame:(CGRect)frame
                         text:(NSString *)text
                    textColor:(UIColor *)textColor
                         font:(UIFont *)font
                textAlignment:(NSTextAlignment)textAlignment
              backgroundColor:(UIColor *)bgColor
{
    SZKLabel *szkLabel=[[SZKLabel alloc]initWithFrame:frame];
    szkLabel.text=text;
    szkLabel.textColor=textColor;
    szkLabel.font=font;
    szkLabel.textAlignment=textAlignment;
    szkLabel.backgroundColor=bgColor;
    return szkLabel;
}
@end

基本上就搞定了。

笔者的其他文章集锦:
iOS知识点分享
//www.greatytc.com/nb/4004224

如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到后第一时间回复。
QQ/微信:790057066 。
简书号:iOS_凯://www.greatytc.com/users/86b0ddc92021/latest_articles
GitHub个人主页(欢迎star):https://github.com/18811314750

感谢各位观众老爷的阅读,如果觉得笔者写的还凑合,可以关注或收藏一下,不定期分享一些好玩的实用的demo给大家。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容