可以简易设置文字内边距的EdgeInsetsLabel
2014-10-27 16:32
155 查看
可以简易设置文字内边距的EdgeInsetsLabel
最终效果:
源码:
EdgeInsetsLabel.h 与 EdgeInsetsLabel.m
ViewController.m
核心原理:
最终效果:
源码:
EdgeInsetsLabel.h 与 EdgeInsetsLabel.m
// // EdgeInsetsLabel.h // EdgeInsetsLabel // // Created by YouXianMing on 14/10/27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @interface EdgeInsetsLabel : UILabel @property(nonatomic, assign) UIEdgeInsets edgeInsets; @end
// // EdgeInsetsLabel.m // EdgeInsetsLabel // // Created by YouXianMing on 14/10/27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "EdgeInsetsLabel.h" @implementation EdgeInsetsLabel - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { UIEdgeInsets insets = self.edgeInsets; CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets) limitedToNumberOfLines:numberOfLines]; rect.origin.x -= insets.left; rect.origin.y -= insets.top; rect.size.width += (insets.left + insets.right); rect.size.height += (insets.top + insets.bottom); return rect; } - (void)drawTextInRect:(CGRect)rect { [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)]; } @end
ViewController.m
// // ViewController.m // SetInsets // // Created by YouXianMing on 14/10/27. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "EdgeInsetsLabel.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; EdgeInsetsLabel *label = [[EdgeInsetsLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; label.edgeInsets = UIEdgeInsetsMake(8, 8 + 10, 8, 8 + 10); // 设置内边距 label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:30.f]; label.text = @"No Zuo No Die"; [label sizeToFit]; // 重新计算尺寸 label.layer.cornerRadius = label.frame.size.height / 2.f; label.backgroundColor = [UIColor blackColor]; label.textColor = [UIColor redColor]; label.layer.masksToBounds = YES; label.center = self.view.center; [self.view addSubview:label]; } @end
核心原理:
相关文章推荐
- c#动态设置combobox控件下拉项宽度以实现下拉项文字可以完全显示。
- (非常好的Word操作封装类)VC中进行office编程的-操作word,可以设置文字的样式,新建表格
- android textview可以设置文字颜色 部分文字点击事件处理
- 推荐一个可以设置图片型PDF背景和文字颜色的阅读器
- iOS小技巧20-iOS UILabel 文字设置垂直居中、垂直靠上、垂直靠下,设置edgeInsets
- 可以设置背景和文字颜色的Static控件
- UIEdgeInsetsMake设置UIButton的图片、文字的相对位置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- WinForm 选项卡在左边(虽然可以直接设置但是文字错误方向,也许是VS2010的BUG)
- iOS - UIButton(UIEdgeInsets) 设置button上的文字和图片
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- IOS UIButton(UIEdgeInsets)/设置图片和文字上下垂直居中对齐
- 设置TextView显示的文字可以复制
- [礼仪大赛策划方案手段]PPT简易的文字编排+网络大量免费的Flash动画+笔记本都具有的扩展桌面双屏支持功能+投影机 就可以打造一场专业的电视台效果时,有何感想?能弃之这些身边的条件而不用吗?
- dede中可以用系统设置中的添加新变量来调用频繁改变的文字内容
- HTML设置表格里文字的边距
- 设置按钮内容的对齐方式、左边距,文字左边距
- UIButton设置文字和图片的位置EdgeInsets