设置label的内边距
来源:互联网 发布:单片机怎么打开 编辑:程序博客网 时间:2024/06/14 13:18
有的地方需要设置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
使用方法:
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); // 设置左内边距,只要设置该属性就可以设置内边距了,.m中的 - (void)drawTextInRect:(CGRect)rect 方法就是实现过程
0 0
- 设置label的内边距
- c# label的设置
- 可设置行距的Label
- 动态设置label的高度
- 设置Label的行间距
- 设置label的行间距
- label的富文本设置
- 设置Label的行间距
- 设置label的描边
- Panel以及Label的设置
- IOS 设置label内的字体大小适应label的宽度
- Label--自定义可调整内边距的Label
- AS3 中Label相关属性的设置
- Cocos2d label对齐的锚点设置
- 如何设置HTML span、label 的宽度
- 自定义Label,设置关键字的颜色
- 设置Label显示百分比变化的NSTimer
- 使用storyboard设置label的自适应高度
- C++ 文件操作
- 第二周项目3-体验复杂度-(2)汉诺塔
- (三)Jetson TK1安装opencv-2.4.9,可能的问题error: a storage class is not allowed in an explicit specialization
- 各种排序算法的分析及java实现
- 173. Binary Search Tree Iterator
- 设置label的内边距
- 读取联系人数据库信息
- Ajax实现省市区三级级联(数据来自mysql数据库)
- 深度学习在目标跟踪中的应用
- CUDA编程(二):基本项目建立流程
- Python中的面向对象编程详解(上)
- 设置UITextView的时候文字居中显示了?
- 愚蠢的错误
- json相关知识