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

//  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

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

推荐阅读更多精彩内容