设置UILabel的内边距
来源:互联网 发布:淘宝商家联系方式提取 编辑:程序博客网 时间:2024/05/16 10:27
最近在项目中,有个地方需要设置UILabel的内边距,即字体和Label控件之间的间隙。UILabel不像UIButton那样,有个contentEdgeInsets、titleEdgeInsets、imageEdgeInsets供我们设置文字或图片与按钮边界的界限,所以我们只能另外想其他办法来实现。其实,办法也很简单,只需要我们自定义UILabel,然后重写drawTextInRect:方法即可实现我们的目标。
CustomLabel.h
#import <UIKit/UIKit.h>@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); // 设置左内边距
0 0
- 设置UILabel的内边距
- 如何设置UILabel的内边距?
- 如何设置UILabel的内边距?
- UILabel的AttributedString设置
- UILabel的所有设置
- 自定义UILabel,设置内边距
- UILabel的相关属性设置
- iOS 设置UILabel的背景图片
- UILabel的相关属性设置
- 设置UILabel的内间距
- UILabel的相关属性设置
- 设置UIlabel的行高
- UILabel的相关属性设置
- 设置UILabel的行间距
- UILabel的相关属性设置
- UIlabel 行间距的设置
- UILabel的相关属性设置
- UILabel常用的属性设置
- C# 抽象类(知识要点归纳总结)
- 51 Nod 1305 Pairwise Sum and Divide (规律)
- node-zk-browser 安装
- 利用StringEscapeUtils对字符串进行各种转义与反转义(Java)
- android_86_AsyncTask
- 设置UILabel的内边距
- jks(私钥)keystore(私钥)truststore(受信任的公钥)cer(公钥)pfx(浏览器);密钥对,公钥,证书,私钥,jks,keystore,truststore,cer,pfx说明
- Linux-磁盘管理基本命令
- c++ Primer Plus(第六版)第十二章习题,写代码之路
- Django list view
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Java动态代理学习
- linux基础知识(三)
- Android BigDecime的巧妙用法