可以简易设置文字内边距的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的更多相关文章

  1. CSS.03 -- 浏览器行高、字体;盒子模型--边框、内边距、外边距

    如果此时你也在自学中,请使用 FireWorks CS6 进行切图测距等,百度一下吧~ Fireworks的基本使用 新建文件   ctrl+n 打开文件  ctrl+o 调出和隐藏标尺 ctrl+r ...

  2. W3School-CSS 内边距 (padding) 实例

    CSS 内边距 (padding) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CS ...

  3. 谈谈tableView的重要属性内边距

    全屏穿透效果需要做到两点 tableView的可视范围占据整个父控件(或者屏幕)--设置contentsize滚动范围. 所有的cell都可以被看到,也就是说tableView中的cell不会被导航栏 ...

  4. CSS 内边距 (padding) 实例

    CSS 内边距 (padding) 实例元素的内边距在边框和内容区之间.控制该区域最简单的属性是 padding 属性. CSS padding 属性定义元素边框与元素内容之间的空白区域.CSS 内边 ...

  5. CSS 内边距 padding 属性

    CSS padding 属性定义元素边框与元素内容之间的空白区域. ㈠padding(填充) ⑴当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背景颜色的填充. ⑵单独使用 p ...

  6. Swift- 设置 UILabel 内边距

    摘要 拿来即用短时间效率虽然挺高的,但是拿来的东西没有消化一次,就无法得心应手的使用它. 这次的探索思路就是,查询官方文档,设置不同的值测试单个方法中参数的变化,之后测试两个方法的执行顺序,处理的思路 ...

  7. iOS 设置UILabel 的内边距

    iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {, , , }; [super draw ...

  8. 设置layui表格cell的内边距

    /*设置layui表格cell的内边距*/ .layui-table-cell { height: 50px !important; line-height: 50px !important; }

  9. 【Swift】UILabel 设置内边距

    前言 对应一个曾经开发 Android 的人来说,没有这些基础属性简直令人发指,还是表喷这个,认真写代码 - - # 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblo ...

随机推荐

  1. elasticsearch插件安装之--中文分词器 ik 安装

    /** * 系统环境: vm12 下的centos 7.2 * 当前安装版本: elasticsearch-2.4.0.tar.gz */ ElasticSearch中内置了许多分词器, standa ...

  2. C语言中的按位移动及其简单引用

    C语言中的按位移动及其简单应用 在C语言中按位左移用”<<”表示,按位右移用”>>”表示. 按位左移和按位右移运算经常被用来替换乘二和除二运算,但是要注意,这两者之间并不完全等 ...

  3. Idea 2017.3以后版本的破解(亲测有效)转

    转自:http://www.mamicode.com/info-detail-2147137.html 自从升级到idea2017.3之后,之前的license server破解方法貌似已失效.于是找 ...

  4. JavaI/O系统2

    数据流: DataInputStream,DataOutputStream.可以用于与计算机无关的格式读写java的基本数据类型以及String对象 对象流: ObjectInputStream,Ob ...

  5. Week6&7——第一次项目冲刺(Alpha版本)

    Deadline: 2017-11-11 10:00PM,以博客发表日期为准. 评分基准: 按时交 - 有分(需求&原型改进与系统设计-10分,敏捷冲刺-70分),检查的项目包括后文的三个方面 ...

  6. dev中文本框等获取焦点事件

    <ClientSideEvents GotFocus="GotFocus" /> editContract.SetFocus()//设置文本框等的焦点 function ...

  7. Xcode8如何创建Framework静态SDK库

    iOS的软件开发工具包SDK,一般是以库的形式出现,从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.开发过程中,我们常常会用到第三方的SDK.尤其是微信.QQ.百度地图.支付功能等.其 ...

  8. AVPlayer 音乐播放后台播放,以及锁屏主题设置

    第一步:在appDelegate中通知app支持后台播放:在方法 - (BOOL)application:(UIApplication *)application didFinishLaunching ...

  9. 虚拟机VMware workstations的网络设置

    一般遇到虚拟机中上不了网的问题,可以这样解决: 1.在终端输入命令:ifconfig.--查看eth0接口上是否有IP地址. 发现eth0接口上没有ip地址. 2.输入cat /etc/sysconf ...

  10. Android开发之旅3:android架构

    引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWorld Android 开发之旅:HelloWorld项目的目录结构 我们对android有了个大致的了解,知道如何搭建andr ...