怎么修改 uibutton图片文字上下 的文字

willingYaTou
阅读(38118)
UIEdgeInsets
typedef struct UIEdgeInsets {
CGFloat top, left, bottom,
// specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
@property(nonatomic)
UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero
@property(nonatomic)
UIEdgeInsets titleEdgeI
// default is UIEdgeInsetsZero
@property(nonatomic)
reversesTitleShadowWhenH // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance
@property(nonatomic)
UIEdgeInsets imageEdgeI
// default is UIEdgeInsetsZero
UIEdgeInsetsMake
里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在button的正中央
UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
UIEdgeInsets insets = {top, left, bottom, right};
self.view.backgroundColor = [UIColor blackColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型
button.frame = CGRectMake(100, 100,90, 90);//button的frame
button.backgroundColor = [UIColor cyanColor];//button的背景颜色
[button setBackgroundImage:[UIImage imageNamed:@&man_64.png&] forState:UIControlStateNormal];
在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
[button setImage:[UIImage imageNamed:@&IconHome@2x.png&] forState:UIControlStateNormal];//给button添加image
button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
[button setTitle:@&首页& forState:UIControlStateNormal];//设置button的title
button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
button.titleLabel.textAlignment = NSTextAlignmentC//设置title的字体居中
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right)
[button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];//
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentC//设置button的内容横向居中。。设置content是title和image一起变化
[button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//button相应的事件
-(void)tap {
NSLog(@&tap a button&);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@&hello& message:@&willingseal& delegate:self cancelButtonTitle:@&ok& otherButtonTitles: nil];
[alertView show];
我这里使用的image是64*64的
那就一个button,这个button的背景图片中既有图片又有文字就可以了,找个美工帮忙弄一下应该很逼真的。。。!!高级丫!!!可是我没有美工!!不会ps。。所以我就只能这样子了
阅读排行榜btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];
//这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最后将按钮加入到指定视图superView
[superView addSubview: btn];
==========================================================
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentL//设置文字位置,现设为居左,默认的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentL
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
=======================================================
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
资源参考:http://blog.csdn.net/chengyingzhilian/article/details/8363855
如果您觉得此文章对你帮助很大,不妨打赏下!
Please enable JavaScript to view the我们在 iOS,可以设置 UIButton 的字体和标题和获取其宽度过程吗?
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
在 iOS,可以我们设置 UIButton 获取其宽度,或必须设置其宽度,然后设置它的内容吗?
看起来我们可以
[someString sizeWithFont:someFont].width
获取一个宽度,然后使它 (通过设置其帧) 按钮的宽度,然后设置该标题和字体。但我们可以只是设置按钮的字体和标题 (和自动调整按钮),然后给宽度代替吗?
解决方法 1:
使用 sizeToFit 方法的 UIView ()
[button setTitle:@"Hello World" forState:UIControlStateNormal];
[button sizeToFit];
float width = button.frame.size.怎么修改 UIButton 的文字_百度知道
怎么修改 UIButton 的文字
提问者采纳
互相理解,可另外向我求助;答题不易,请继续“追问”.,还有不懂的地方button 添加个方法 在里面设置button的title你的采纳是我前进的动力。 如你还有别的问题.,
网络工程师
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁二次元同好交流新大陆
扫码下载App
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(8308)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'设置UIButton 字体 颜色',
blogAbstract:'摘自:/fuxiaohui@126/blog/static/9/设置UIButton上字体的颜色,不是用: & & [payWayButton.titleLabel setTextColor:[UIColor blackColor]]; ',
blogTag:'uibutton字体颜色',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:5,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:3,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 ios uibutton 文字 的文章

 

随机推荐