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

核心原理:

时间: 2024-08-01 18:04:02

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

38 内边距 padding 1 padding-top right left bottom 2 盒子的大小 3 设置padding内边距 子元素默认都是在父元素的内容区

1 2   原文地址:https://www.cnblogs.com/anvivi/p/9708687.html

CSS 内边距 padding 属性

CSS padding 属性定义元素边框与元素内容之间的空白区域. ㈠padding(填充) ⑴当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背景颜色的填充. ⑵单独使用 padding 属性可以改变上下左右的填充.   ⑶可能的值: ⑴length    定义一个固定的填充(像素, pt, em,等) ⑵%          使用百分比值定义一个填充 注意:padding 属性接受长度值或百分比值,但不允许使用负值.   ⑷示例1:如果你希望所有 h1 元素的各边都有

iOS 设置UILabel 的内边距

iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } 参考:http://stackoverflow.com/questions/3476646/uilabel-text-margin http://unmi.cc/uilable-uitext

【Swift】UILabel 设置内边距

前言 对应一个曾经开发 Android 的人来说,没有这些基础属性简直令人发指,还是表喷这个,认真写代码 - - # 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 class UILabelPadding : UILabel { private var padding = UIEdgeInsetsZero @IBInspectable var paddingLeft: CGF

xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

代码创建 //创建UIButton UIButton * btnType=[[UIButton alloc]init]; //设置UIControlStateNormal状态下的文字颜色 [btnType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //设置字体大小 btnType.titleLabel.font=[UIFont systemFontOfSize:9.0]; //设置边框的宽度 btnTyp

css内边距与外边距的区别

你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用 padding而不是margin?你知道负margin吗?你知道负margin在实际工作中的用途吗?常见的浏览器下margin出现的bug有哪些?…… 写css,你少不了与margin打交道,而对于这个平时我们最常用的css属性我们并非十分了解.介于此我打算写下这篇文章,一来是自己工作中的总结,也是对自己知识的一次梳理. Margin是什么 CSS 边距属性

UILabel实现上下左右内边距和自适用高度的计算三种方法

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000; background-color: rgba(0, 0, 0, 0) } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #000000; background-color: rgba(0, 0, 0

CSS之盒模型边框,内边距,外边距,阴影,圆角

盒子模型 盒子模型 盒子模型有元素内容.边框(border).内边距(padding).外边距(margin)组成: 盒子里面的文字和图片等元素是内容区域: 盒子的厚度 我们称为 盒子的边框: 盒子内容与盒子之间的距离是内边距: 盒子与盒子之间的距离是外边距: 标准盒子模型 盒子边框(border) border: border-width || border-style || border-color 属性 作用 border-width 定义边框粗细,单位是 px border-style

UI基础——按钮内边距,图片拉伸

一.内边距 UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距 1.contentEdgeInsets 如果是设置contentEdgeInsets, 会把UIImageView和UIlabel当做一个整体移动 btn.contentEdgeInsets = UIEdgeInsetsMake(30, 0, 0, 0); 对应状态: 2.titleEdgeInsets/imageEdgeInsets 如果是设置titleEdgeInsets/imageEdgeInsets. 那么不