如何设置UILabel的内边距?
来源:互联网 发布:快拍王软件下载 编辑:程序博客网 时间:2024/04/30 13:54
最近在项目中,有个地方需要设置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如何设置成透明
- UILabel的AttributedString设置
- UILabel的所有设置
- 如何获得UILabel在设置text后的真实长度
- 如何获得UILabel在设置text后的真实长度
- 自定义UILabel,设置内边距
- UILabel的相关属性设置
- iOS 设置UILabel的背景图片
- UILabel的相关属性设置
- 设置UILabel的内间距
- UILabel的相关属性设置
- 设置UIlabel的行高
- UILabel的相关属性设置
- 设置UILabel的行间距
- UILabel的相关属性设置
- 架构师系列(一)--IT行业架构师之我初见
- 48.Which two statements are true regarding indexes? (Choose two.)
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- 2、发现群组 Python代码
- 英语总结
- 如何设置UILabel的内边距?
- Java中的DatagramPacket与DatagramSocket的初步
- c++ const类成员函数
- TextView 文字加上闪光与外层圆跟内层圆显示文字
- 用systemd命令来管理linux系统
- muduo库的学习8---TcpServer与TcpClient的基本设计---TcpServer
- 数据库备份脚本使用说明
- 反射实现对象改值
- 1-Python 安装 Matplotlib