UILabel设置内边距
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UILabel设置内边距相关的知识,希望对你有一定的参考价值。
参考技术A CustomLabel.h#import
@interface CustomLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字体与控件边界的间隙
@end
CustomLabel.m
#import "CustomLabel.h"
@implementation CustomLabel
- (instancetype)init
if (self = [super init])
_textInsets = UIEdgeInsetsZero;
return self;
- (instancetype)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
_textInsets = UIEdgeInsetsZero;
return self;
- (void)drawTextInRect:(CGRect)rect
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, _textInsets)];
@end
Demo.m
CustomLabel *titleLabel = [[CustomLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.f, 24.0f)];
titleLabel.backgroundColor = [UIColor whiteColor];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:12.0f];
titleLabel.textInsets = UIEdgeInsetsMake(0.f, 15.f, 0.f, 0.f); // 设置左内边距
以上是关于UILabel设置内边距的主要内容,如果未能解决你的问题,请参考以下文章