可以简易设置文字内边距的EdgeInsetsLabel
可以简易设置文字内边距的EdgeInsetsLabel

最终效果:


源码:
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(, , , )];
label.edgeInsets = UIEdgeInsetsMake(, + , , + ); // 设置内边距
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
label.text = @"No Zuo No Die";
[label sizeToFit]; // 重新计算尺寸
label.layer.cornerRadius = label.frame.size.height / .f;
label.backgroundColor = [UIColor blackColor];
label.textColor = [UIColor redColor];
label.layer.masksToBounds = YES;
label.center = self.view.center; [self.view addSubview:label];
} @end
核心原理:

可以简易设置文字内边距的EdgeInsetsLabel的更多相关文章
- CSS.03 -- 浏览器行高、字体;盒子模型--边框、内边距、外边距
如果此时你也在自学中,请使用 FireWorks CS6 进行切图测距等,百度一下吧~ Fireworks的基本使用 新建文件 ctrl+n 打开文件 ctrl+o 调出和隐藏标尺 ctrl+r ...
- W3School-CSS 内边距 (padding) 实例
CSS 内边距 (padding) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CS ...
- 谈谈tableView的重要属性内边距
全屏穿透效果需要做到两点 tableView的可视范围占据整个父控件(或者屏幕)--设置contentsize滚动范围. 所有的cell都可以被看到,也就是说tableView中的cell不会被导航栏 ...
- CSS 内边距 (padding) 实例
CSS 内边距 (padding) 实例元素的内边距在边框和内容区之间.控制该区域最简单的属性是 padding 属性. CSS padding 属性定义元素边框与元素内容之间的空白区域.CSS 内边 ...
- CSS 内边距 padding 属性
CSS padding 属性定义元素边框与元素内容之间的空白区域. ㈠padding(填充) ⑴当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背景颜色的填充. ⑵单独使用 p ...
- Swift- 设置 UILabel 内边距
摘要 拿来即用短时间效率虽然挺高的,但是拿来的东西没有消化一次,就无法得心应手的使用它. 这次的探索思路就是,查询官方文档,设置不同的值测试单个方法中参数的变化,之后测试两个方法的执行顺序,处理的思路 ...
- iOS 设置UILabel 的内边距
iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {, , , }; [super draw ...
- 设置layui表格cell的内边距
/*设置layui表格cell的内边距*/ .layui-table-cell { height: 50px !important; line-height: 50px !important; }
- 【Swift】UILabel 设置内边距
前言 对应一个曾经开发 Android 的人来说,没有这些基础属性简直令人发指,还是表喷这个,认真写代码 - - # 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblo ...
随机推荐
- 常用工具说明--jsdoc 前端文档输出工具
1.利用npm安装jsdoc模块 npm install jsdoc -g 2.由于jsdoc默认的生成文档模板不好看,可以下载一套好看些的模板,如 jaguar.js 下载地址:https:// ...
- jQuery操作<input type="radio">
input type="radio">如下: <input type="radio" name="city" value=&qu ...
- ABP学习入门系列(五)(展示实现增删改查)
大致要实现的 效果如下 1,添加Controller(用到的X.PagedList 注意到nuget添加) using System.Web.Mvc; using Abp.Application.Se ...
- 构造用于1个方法在多线程环境下重复多次执行测试的通用方法 C#
static void MultithreadsCycleTestWithSameMethod(int threadNumber, int cycleNumber, ThreadStart actio ...
- The request sent by the client was syntactically incorrect.
HTTP Status 400 - type Status report message description The request sent by the client was syntacti ...
- Q:链表的中间元素
问题:如何得到一个链表的中间元素? 相信,这个问题对于刚入门编程的人来说,都不会难,最自然而然的想法是先遍历一遍链表,统计链表中的元素的个数,之后,再走一遍链表,所走的步长为链表长度的一半.这样就 ...
- YII使用beanstalk队列
转载于:http://blog.csdn.net/yao970953039/article/details/41821387 1.系统centos 我是直接使用yum install beanstal ...
- NOIP2017 题解
QAQ--由于没报上名并没能亲自去,自己切一切题聊以慰藉吧-- 可能等到省选的时候我就没有能力再不看题解自己切省选题了--辣鸡HZ毁我青春 D1T1 小凯的疑惑 地球人都会做,懒得写题解了-- D1T ...
- CentOS /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
使用的时候出现一个错误 bash: /usr/local/bin/rar: /lib/ld-linux.so.2: bad ELF interpreter: No such file or direc ...
- ArcEngine对Blob字段赋值的方法
今天在测试数据入库程序,发现对某个图层操作之后,调用StopOperation,会出现“尝试写入或读取受保护的内存”错误. 经过测试,最终发现是因为该图层包含有Blob字段,而代码没有专门对Blob字 ...