[转]设置 UILabel 和 UITextField 的 Padding 或 Insets
2019独角兽企业重金招聘Python工程师标准>>>
iOS 的控件,只看到 UIButton 可以设置 Padding/Insets,即按钮上文字或图片与按钮边界的间隙,对与 CSS 来说叫做 Padding,在 iOS 中叫做 Insets,UIButton 设置 Insets 相应的属性如下:
Configuring Edge Insets
contentEdgeInsets property
titleEdgeInsets property
imageEdgeInsets property
它们接受的属性类型是:UIEdgeInsets,由函数 UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right ); 构造出,分别表示其中的内容/标题/图片离各边的距离。
在 xib 中也有界面来对按钮的这三个 EdgeInsets 属性的设置,分别是按钮的 Edge 和 Inset 属性。
印像中,Swing 的许多组件都可设置 Insets 属性,可对于 iOS 的控件就没那么幸运了,比如我想设置 UILable 或 UITextField 中的文本离边界的间隙,无伦是在 xib 里还是直接代码的方式都无能为力,因为它们根据未开放相应的属性让你去控制。
办法当然还是有的,自定义相应自己的控件了,比如 InsetsLabel 或是 InsetsTextField,接着就是覆盖某些个方法来达成。
首先来看 UILabel 的子类 InsetsLabel 的实现代码:
//1.header file
#import <UIKit/UIKit.h>@interface InsetsLabel : UILabel
@property(nonatomic) UIEdgeInsets insets;
-(id) initWithFrame:(CGRect)frame andInsets: (UIEdgeInsets) insets;
-(id) initWithInsets: (UIEdgeInsets) insets;
@end//2. implementation file
#import "InsetsLabel.h"@implementation InsetsLabel
@synthesize insets=_insets;
-(id) initWithFrame:(CGRect)frame andInsets:(UIEdgeInsets)insets {self = [super initWithFrame:frame];if(self){self.insets = insets;}return self;
}-(id) initWithInsets:(UIEdgeInsets)insets {self = [super init];if(self){self.insets = insets;}return self;
}-(void) drawTextInRect:(CGRect)rect {return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}
关键就是覆盖了 -(void) drawTextInRect: (CGRect) rect; 方法,在画 Label 的文本时分别设置文本与 Label 四个边的间隙,即画在 Label 内的一个小矩形内,这个例子提供了便利的构造函数,提供自己的 UIEdgeInsets 属性。另外,函数 UIEdgeInsetsInsetRect(CGRect, UIEdgeInsets) 应该是好理解的。
再看如何设置 UITextField 中文本到四边的间距,这里也可以定义自己的 InsetsTextField:
//
// Created by Unmi on 11/2/11.
// Copyright (c) 2011 http://unmi.cc. All rights reserved.
//#import <UIKit/UIKit.h>@interface InsetsTextField : UITextField
@end@implementation InsetsTextField
//控制 placeHolder 的位置,左右缩 20
- (CGRect)textRectForBounds:(CGRect)bounds {return CGRectInset( bounds , 20 , 0 );
// CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height); //更好理解些
// return inset;
}// 控制文本的位置,左右缩 20
- (CGRect)editingRectForBounds:(CGRect)bounds {return CGRectInset( bounds , 20 , 0 );
//CGRect inset = CGRectMake(bounds.origin.x + 10, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
// return inset;
}
@end//-----------------------------------------------------------------
//下面是使用 InsetsTextField 的代码,可放在 viewDidLoad 等代理方法中
InsetsTextField *insetTextField = [[InsetsTextField alloc]initWithFrame:CGRectMake(10, 10, 180, 25)];//须手动设置它的 borderStyle, 不然看不到边框的
insetsTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:insetsTextField];
[insetsTextField release];
效果如下:
上面更像是借鉴的 InsetsLabel 的实现,其实对于 UITextField 还有更好的实现办法,而且更简单,因为 UITextFiled 原来就支持的做法。比如它可以让你做出在文本框最前方固定一个 $ 符号,表示这个文本框是输入钱的,第一个$ 是不能被删除的。确实,你可以在 TextField 上贴个 Label,然后文本框的光标后移,稍显麻烦了。
而 UITextField 可以直接设置 leftView 或 rightView, 然后文本输入区域就在 leftView 和 rightView 之间了,看例子:
UILabel *paddingView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 25)];
paddingView.text = @"$";
paddingView.textColor = [UIColor darkGrayColor];
paddingView.backgroundColor = [UIColor clearColor];
textfield.leftView = paddingView;
textfield.leftViewMode = UITextFieldViewModeAlways;
rightView 也是一样的设置方式,其中的 Mode 有四种,看到名字应该不难理解:
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
它的效果呢就更酷了:
文本框的起始光标是从上图数字 1 位置开始的。
实际应用中,对于 UITextField 如果有类似的需求,我会毫不犹豫的使用 leftView/rightView 属性来设置。
参考:1. http://stackoverflow.com/questions/2694411/text-inset-for-uitextfield
2. http://stackoverflow.com/questions/5674655/uitextfield-align-left-margin
转载于:https://my.oschina.net/leeming/blog/51818
[转]设置 UILabel 和 UITextField 的 Padding 或 Insets相关推荐
- UI基础控件创建(UILabel、UITextField、UIButton)
UI基础控件创建(UILabel.UITextField.UIButton) UILabel //UILabel;UILabel *nameLabel = [[UILabel alloc] init] ...
- 学习IOS开发UI篇--UIView\UIButton\UILabel\UIImageView\UITextField
UIView\UIButton\UILabel\UIImageView\UITextField,这些都是UI中常用的比较简单地类. 所有的UI类都继承自UIView,所以都有共同的属性,frame,b ...
- iOS 设置UILabel 的内边距
iOS 设置UILabel 的内边距 - (void)drawTextInRect:(CGRect)rect {UIEdgeInsets insets = {0, 5, 0, 5};[super dr ...
- Swift - 设置UILabel、UITextView的文字行间距
有时我们需要调整 label 或 textView 的文本行间距大小,但这两个组件都没有相关属性可以直接设置.这个就需要借助富文本( NSAttributedString )来实现. 一.设置 ...
- 如何设置UILabel的内边距?
最近在项目中,有个地方需要设置UILabel的内边距,即字体和Label控件之间的间隙.UILabel不像UIButton那样,有个contentEdgeInsets.titleEdgeInsets. ...
- ios label文字行间距_iOS 设置UILabel行间距【原创】
今天在项目中遇到需要设置较多文字的行间距问题,得到解决办法分享给大家. 下面贴出代码供参考: 1.正常使用 UILabel *label = [[UILabel alloc] initWithFram ...
- UI基本控键UIView ,UILabel,UITextField ,UIButton,UIAlertView
视频(ffmpeg),即时通讯(需要服务器支持,需要socket通信协议)技术非常重要 .. 1.UIWindow --窗口类.. UIScreen 屏幕类.. UIColor 颜色类 vie ...
- iOS 设置UILabel 的行间距
// // UILabel+LineSpace.h//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface UILabel (L ...
- 设置UILabel可变高度(根据文本内容自动适应高度)
@property(nonatomic)UILabel *showLabel; // 计算文本所占高度,计算出来之后设置label的高度 // 第一个参数:字体大小,字体大小/样式影响计算字体的高度 ...
最新文章
- 【Java】 leetCode 删除链表中等于给定值 val 的所有节点。
- linux 初始化工作进程 systemd简介
- LeetCode Minimum Depth of Binary Tree
- DEV—【GridControl添加按钮列】
- Linux信号 三 信号发送接口集合
- C#数据结构-稀疏矩阵
- 日文邮件变成乱码解决方案
- dropbox离线安装版下载方法
- 外设键盘_记得那个被称为‘顶级外设’的国产品牌吗,现在推出这样一把键盘...
- sql查询初学者指南_面向初学者SQL Server查询执行计划–类型和选项
- 传感器的定义、构成、分类
- Mysql8.0.12解压版安装亲测(步骤超级简单)
- Matlab遗传算法工具箱(gaot)下载及安装
- NI 国家仪器 各版本软件下载链接
- python绘制相频特性曲线_数据分析之Matplotlib和机器学习基础
- 2014腾讯校园招聘实习笔试题
- canvas生成二维码海报-可配置
- ppt播放动画花屏-问题解决
- ViewPager简单介绍(一)
- 买服务器做网站 镜像选什么,如何做网站镜像,网站镜像方法
热门文章
- python属性访问顺序_Python 对象属性的访问
- 二十年后我发明了保姆机器人作文_五年级作文:二十年后的家乡(张羽彤)
- amba simple class驱动_学习笔记:class加载器和双亲委派模型
- mysql jdbc批量更新_jdbc批量更新数据
- Leetcode69场双周赛-第三题5962. 连接两字母单词得到的最长回文串
- python计算能够包含两个圆的最小圆
- 信息系统项目管理师论文范文-质量管理
- 笔记-项目范围管理-需求工程-需求分类
- ElementUI中的el-table怎样实现每一列显示的是控件并能动态实现双向数据绑定
- SpringBoot+POI实现导入Excel时验证并返回错误Cell标红的文件