美文网首页
iOS之UIlabel中让里面的字体左右空出一段距离的方法

iOS之UIlabel中让里面的字体左右空出一段距离的方法

作者: i得深刻方得S | 来源:发表于2016-08-10 15:32 被阅读354次

//  Created by guoya song on 16/8/10.

//  Copyright © 2016年 guoyasong. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@interface TestUILabel : UILabel

@end

@implementation TestUILabel

- (void)drawTextInRect:(CGRect)rect {

//文字距离上下左右边框都有10单位的间隔

CGRect newRect = CGRectMake(rect.origin.x + 10, rect.origin.y + 10, rect.size.width - 20, rect.size.height -20);

[super drawTextInRect:newRect];

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UILabel *myLabel=[[TestUILabel alloc]initWithFrame:CGRectMake(100,100, 100, 20)];

myLabel.text=@"我的三";

myLabel.layer.borderWidth=1;

[myLabel drawTextInRect:myLabel.frame];

[self.view addSubview:myLabel];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

相关文章

网友评论

      本文标题:iOS之UIlabel中让里面的字体左右空出一段距离的方法

      本文链接:https://www.haomeiwen.com/subject/qakjsttx.html