如何设置UILabel的内边距?
来源:互联网 发布:微星淘宝旗舰店叫什么 编辑:程序博客网 时间:2024/05/16 13:49
最近在项目中,有个地方需要设置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的相关属性设置
- 【备忘】李炎恢老师HTML5+CSS3教程与课件代码【共享完毕】下载
- Centos7下安装qt5.8.0 静态编译库
- Ubuntu下安装CodeBlocks
- Android第三方等待框SpinKit
- API简介
- 如何设置UILabel的内边距?
- ios json数据转plist文件
- Map-HashMap
- Android Studio-设置快速修复错误提示代码 相当于eclipse F1 快捷键
- OpenCV配置使用过程中遇到的问题
- 关于fread函数读取到的数据和实际统计的和数据不一样
- MySQL
- 让标签内部的内容在一行显示,不随着浏览器大小换行
- linux基础协议和端口号(后期补充)