可以简易设置文字内边距的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. Term Weighting

    对文本分词后,接下来需要对分词后的每个term计算一个权重,重要的term应该给与更高的权重.举例来说,“什么产品对减肥帮助最大?”的term weighting结果可能是: “什么 0.1,产品 0 ...

  2. ssh和ssh-copy-id以及批量多机无密码登陆详解

    本文主要围绕着ssh服务以及如何通过ssh-copy-id实现无密码登陆. 1. sshd 服务以及配置   2.ssh-copy-id命令的使用以及原理.3.批量多机互相信任. 1. sshd 服务 ...

  3. php里的二进制安全

    二进制安全功能(binary-safe function)是指在一个二进制文件上所执行的不更改文件内容的功能或者操作.这能够保证文件不会因为某些操作而遭到损坏.二进制数据是按照一串0和 1的形式编码的 ...

  4. Fiddler——PC上实现手机的抓包(转载 http://www.jianshu.com/p/13f8a81d7c7c)

    Fiddler是15年初,在千牛中做超级促销插件时,发现没有root的Android机和没有越狱的iPhone无法修改host,因此没办法测试.为了让我这个磨人的PD也能看到,开发推荐了Fiddler ...

  5. 编写高质量 JavaScript -- 知识点小记

    一: 团队合作避免JS冲突 脚本中的变量随时存在冲突的风险, 1.   解决办法---用匿名函数将脚本包起来,让变量的作用域控制在匿名函数之内 如: <script type="tex ...

  6. java Html 转 PDF

    Html 转 PDF 使用 flying-saucer 插件来完成 导入flying-saucer依赖 <dependency> <groupId>org.xhtmlrende ...

  7. 每天一道剑指offer-二叉树的下一个结点1

    题目 每天一道剑指offer-二叉树的下一个结点https://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&a ...

  8. [BZOJ 2655]calc

    Description 题库链接 给出 \(A,n,p\) ,让你在模 \(p\) 意义下求所有序列 \(a\) 满足"长度为 \(n\) 且 \(a_i\in[1,A]\) ,并且对于 \ ...

  9. springboots Helloworld

    1.eclipse gradle 插件 HELP----MarketPlace----搜索 buildship点击安装 WINDOW----preferences--gradle 配置安装好的grad ...

  10. CLR 中 线程的 ThreadState 解释

    ThreadState   Aborted 线程已停止 AbortedRequested 线程的 Thread.Abort() 方法已被调用,但线程还未停止. Background 线程在后台执行,与 ...