可以简易设置文字内边距的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 ...
随机推荐
- plt绘制 2维、3维散点图
# 3维import numpy as np import matplotlib.pyplot as plt from sklearn.datasets.samples_generator impor ...
- BiLSTM+CRF 实体识别
https://www.cnblogs.com/Determined22/p/7238342.html 这篇博客 里面这个公式表示抽象的含义,表示的是最后的分数由他们影响,不是直观意义上的相加. 为什 ...
- ID3、C4.5和CART决策树对比
ID3决策树:利用信息增益来划分节点 信息熵是度量样本集合纯度最常用的一种指标.假设样本集合D中第k类样本所占的比重为pk,那么信息熵的计算则为下面的计算方式 当这个Ent(D)的值越小,说明样本集合 ...
- 几句话说说跨IDC分布式数据库Calvin
CalvinFS拿了FAST 15最佳论文:找到了失联十三年的小伙伴:年终/年初整理资料,发现做团委工作的 King 师兄对Calvin有兴趣:最近其他团队对分布式事务和存储问题/兴趣较多……几件事激 ...
- 使用Koa2搭建web项目
随着Node.js的日益火热,各种框架开始层出不穷的涌现出来,Node.js也开始逐渐的被应用到处理服务端请求的场景中.搭建Web项目的框架也随之开始出现——express.koa.koa2.egg等 ...
- koa2开发入门
一.koa2入门 1.创建koa2工程 首先,我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js,输入以下代码: // 导入koa,和koa 1.x不同, ...
- c# combobox控件的使用
POJO: class ComboBoxItem { string _text; string _value; public string Text { get { return _text; } s ...
- CodeBlocks "no such file or directory" 错误解决方案(创建类找不到头文件)
在CodeBlocks下,有时候需要自己定义类,当然就要添加相应的头文件,但添加进去的头文件明明包含在项目中了, 但编译时还是会报错:no such file or directory;这是为什么呢? ...
- AngularJS内建服务以及自定义服务的用法
在AngularJS中, 服务是一个比较重要的部分,它是一个对象或者是函数,可以在你的AngularJS的应用中使用.接下来介绍几种比较常用的内建服务以及自定义服务的方法. [内建服务] (1)loc ...
- [选译]MySQL5.7以上Zip版官方安装文档
前言 在windows上安装Zip版MySQL(选译) 学习mysql的朋友们会发现5.7+版本的mysql变得比以前难安装了许多(当然我们可以选择installer版本,但是这样总感觉对学习mysq ...