iOS-UILabel中文字与边框间距的自定义
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-UILabel中文字与边框间距的自定义相关的知识,希望对你有一定的参考价值。
参考技术A .h文件----------------------------------------------------------------------------------------------------------------
#import
@interface ShowLabel : UILabel
@property ( nonatomic , assign ) UIEdgeInsets edgeInsets;
@end
----------------------------------------------------------------------------------------------------------------
.m文件
----------------------------------------------------------------------------------------------------------------
#import "ShowLabel.h"
@implementation ShowLabel
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
UIEdgeInsetsinsets = self .edgeInsets;
CGRect rect = [ super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)
limitedToNumberOfLines:numberOfLines];
rect.origin.x -= insets.left;
rect.origin.y -= insets.top;
rect.size.width += (insets.left+ insets.right);
rect.size.height+= (insets.top+ insets.bottom);
return rect;
- ( void )drawTextInRect:(CGRect)rect
[ super drawTextInRect:UIEdgeInsetsInsetRect(rect, self .edgeInsets)];
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
// Drawing code
*/
@end
----------------------------------------------------------------------------------------------------------------
使用方法和系统方法一样
ShowLabel*areaLb1=[[ShowLabel alloc]init];
[cell.contentViewaddSubview:areaLb1];
areaLb1.edgeInsets=UIEdgeInsetsMake(5,5,5,5);//设置内边距
[areaLb1sizeToFit];//重新计算尺寸,会执行Label内重写的方法
以上是关于iOS-UILabel中文字与边框间距的自定义的主要内容,如果未能解决你的问题,请参考以下文章