UIButton文字和图片偏移

UIButton 设置偏移

@propery  titleEdgeInsets

@propery  imageEdgeInsets

@propery  contentEdgeInsets

@propery  contentHorizontalAlignment

初始化Button

1    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
2     NSString *title = @"音乐";
3     UIImage  *image = [UIImage imageNamed:@"sp_music_list_sc"];
4     [button setTitle:title forState:UIControlStateNormal];
5     [button setImage:image forState:UIControlStateNormal];
6     button.frame = CGRectMake(20, 100, 100, 40);
7     button.backgroundColor = [UIColor grayColor];

设置文字偏移

1   CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:10.f]];
2     CGSize imageSize = image.size;
3     button.titleEdgeInsets = UIEdgeInsetsMake(15, -imageSize.width, 0, 0);

设置图片偏移

1 button.imageEdgeInsets = UIEdgeInsetsMake(-15,titleSize.width+10,0,0);

设置整体偏移 (文字和图片都会偏移)

1 button.contentEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);

设置对齐方式 (UIButton)

1 //对齐方式  设置content是title和image一起变化
2     button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
时间: 2024-08-04 05:11:47

UIButton文字和图片偏移的相关文章

代码设置UIButton文字、图片位置

假设有按钮rButton的 imageEdgeInsets和contentEdgeInsets可以设置按钮的标题和图片的位置,如下代码,设置标题居右 NSString * rBtnTitle = @"删除"; CGSize size = [rBtnTitle sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]}]; NSLog(@"size = %@",NSStringFromC

iOS·UIButton如何文字在下图片在上

创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 1 -(void)layoutSubviews { 2 [super layoutSubviews]; 3 4 // image center 5 CGPoint center; 6 center.x = self.frame.size.width/2; 7 center.y = self.imageView.frame.s

IOS总结_实现UIButton的图文混排,同时显示文字和图片

我们常常需要用到按钮需要文字和图片同时显示,例如: 我们需要定义一个UIButton的子类 实现文件 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //可根据自己的需要随意调整 self.titleLabel.textAlignment=NSTextAlignmentRight; self.titleLabel.font=[UIFont systemFontOfSize:14

UIButton 文字图片排列

UIButton默认的是:图片-文字水平排列,所以不需要进行调整. 1.文字-图片 水平显示,文字在前,图片再后 [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgArrow.size.width, 0, imgArrow.size.width)]; [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.boun

UIButton的竖排图片和文本

UIButton的竖排图片和文本html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; } .CodeMirror-lines { padding: 4px 0px; } .CodeMirror pre { padding: 0px 4px; } .CodeMirror-scrollbar

UITableView侧滑效果(1)上面文字下面图片的效果

//自定义TableviewCell #import "OrderViewCell.h" #import "Masonry.h" @implementation OrderViewCell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifi

ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Imaging; private void AddTextToImg(string fileName,string text) { if(!File.Exists(MapPath(fileName))) { throw new FileNotFoundException("The file don't

Bootstrap 3之美03-独立行,文字环绕,图片自适应,隐藏元素

本篇主要包括: ■  添加独立的一行■  文字环绕■  图片自适应■  隐藏元素 添加独立的一行 在id为body的section和id为main的section之间,添加2张图片. 我们发现,新加的2张图片把主体内容挤到了右侧. 那么,我们如何处理新加的2张图片呢?--我们不太可能用container,因为它是页面布局层面的类名.但我们可以把这2张图片放在class名为row的div中. 我们还可以加更多的图片. 现在的图片还没有居中.考虑到总共有12个单元格,而4张图片只占了8个单元格,还空

怎么编辑PDF里的文字与图片

总是有很多人一直在网上提问:PDF文件怎么修改文字.PDF文件如何编辑图片等问题.之所以不能编辑,那是因为大家一直都习惯于用PDF阅读器来打开PDF文件,而它并没有编辑PDF文件的权限.想要编辑PDF文件,还得用拥有编辑权限的PDF编辑器.下面跟着小编一起来看下PDF编辑器怎么编辑PDF里的文字与图片.       准备工作:电脑中下载一款叫做迅捷PDF编辑器的工具,下载后将其安装在电脑中,单击"立即体验"即可进入软件中心. 打开文件:单击软件左上方的"打开"按钮,