如何设置UILabel的内边距?

如何设置UILabel的内边距?

2023年7月14日发(作者:)

如何设置UILabel的内边距?最近在项⽬中,有个地⽅需要设置UILabel的内边距,即字体和Label控件之间的间隙。UILabel不像UIButton那样,有个contentEdgeInsets、titleEdgeInsets、imageEdgeInsets供我们设置⽂字或图⽚与按钮边界的界限,所以我们只能另外想其他办法来实现。其实,办法也很简单,只需要我们⾃定义UILabel,然后重写drawTextInRect:⽅法即可实现我们的⽬标。CustomLabel.h#import @interface CustomLabel : UILabel@property (nonatomic, assign) UIEdgeInsets textInsets; // 控制字体与控件边界的间隙@endCustomLabel.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)];}@mLabel *titleLabel = [[CustomLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.f, 24.0f)];oundColor = [UIColor whiteColor];lor = [UIColor blackColor]; = [UIFont systemFontOfSize:12.0f];sets = UIEdgeInsetsMake(0.f, 15.f, 0.f, 0.f); // 设置左内边距

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689264966a226446.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信