iOSUILable边距设置

iOS中Lable是没有UIEdgeInsets这个属性可以调用的,那么我们想修改下Lable的上下左右的边距该怎么办呢?例如:

那么现在来实现下,代码如下:

1.首先创建一个继承UILable的类

2.增加UIEdgeInsets属性


#import 

@interfacecustomBaseLab : UILabel

/**

* lable文字的边距

*/

@property(nonatomic, assign) UIEdgeInsets textLableInsets;

@end

3..m实现如下:


- (instancetype)init {

if(self= [superinit]) {

_textInsets = UIEdgeInsetsZero;

}

returnself;

}

- (instancetype)initWithFrame:(CGRect)frame {

if(self= [superinitWithFrame:frame]) {

_textInsets = UIEdgeInsetsZero;

}

returnself;

}

- (void)drawTextInRect:(CGRect)rect {

[superdrawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];

}

是不是很简单呢 哈哈 !!!!

使用实例:


customBaseLab*yearLab = [[customBaseLaballoc]initWithFrame:CGRectMake(0,0,self.viewWidth-30,self.viewHeight)];

yearLab.backgroundColor= [UIColorwhiteColor];

yearLab.text=@"2012";

yearLab.textColor= [UIColorgrayColor];

yearLab.font= [UIFontsystemFontOfSize:16.0f];

yearLab.textInsets= UIEdgeInsetsMake(0,15,0,0);//调用

[self.viewaddSubview:yearLab];

技术有限 就到这,请大神多多指点;转载请注明出处,谢谢!!!

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

推荐阅读更多精彩内容