如何改变uibutton标题颜色的标题

Uibutton 标题文字位置怎么靠下方_百度知道5821人阅读
今天发现动态改变button title时会出现闪动.很蛋疼的问题,后来经过反复尝试,发现一个解决方案.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC,
dispatch_source_set_event_handler(timer, ^{
NSInteger scondes = timeout % 60;
dispatch_async(dispatch_get_main_queue(), ^{
[_timerBtn setBackgroundColor:[UIColor blueColor]];
_timerBtn.titleLabel.text = [NSString stringWithFormat:@&%ld秒&,scondes];
[_timerBtn setTitle:[NSString stringWithFormat:@&%ld秒&,scondes] forState:UIControlStateNormal];
timeout --;
dispatch_resume(timer);
注意这个方法里
_timerBtn.titleLabel.text = [NSString stringWithFormat:@&%ld秒&,scondes];
[_timerBtn setTitle:[NSString stringWithFormat:@&%ld秒&,scondes] forState:UIControlStateNormal];
如果单独设置_timerBtn.titleLabel.text = [NSString stringWithFormat:@&%ld秒&,scondes];赋值,label是不显示文字的.如果只设置[_timerBtn setTitle:[NSString stringWithFormat:@&%ld秒&,scondes] forState:UIControlStateNormal];这个的话,就会出现闪动的效果..后来经过尝试,发现必须两个都存在,而且_timerBtn.titleLabel.text = [NSString stringWithFormat:@&%ld秒&,scondes];这个要放在[_timerBtn setTitle:[NSString stringWithFormat:@&%ld秒&,scondes] forState:UIControlStateNormal];前边,至于为什么这么做我也没找到合适的解释...
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:92793次
积分:1194
积分:1194
排名:千里之外
原创:27篇
转载:25篇
评论:17条
(1)(1)(1)(3)(3)(4)(6)(2)(3)(2)(1)(4)(6)(1)(3)(3)(1)(1)(8)IOS&开发之设置UIButton的title
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=UIControlContentHorizontalAlignmentLeft&;//设置文字位置,现设为居左,默认的是居中
[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];
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。如何通过代码设置Button title的字体大小_百度知道改变UIButton上标题位置、文字颜色、背景色等 - 推酷
改变UIButton上标题位置、文字颜色、背景色等
示例代码+注释
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
//添加点击事件
[button addTarget:self action:@selector(pushToSecond) forControlEvents:UIControlEventTouchUpInside];
//整个按钮的背景色(无背景图片时有效果)
button.backgroundColor = [UIColor greenColor];
//添加背景图片
[button setBackgroundImage:[UIImage imageNamed:@&01.png&] forState:UIControlStateNormal];
//按钮标题
[button setTitle:@&Hello World!& forState:UIControlStateNormal];
//button上子控件的水平对齐方式
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentL
//button上子控件的垂直对齐方式
button.contentVerticalAlignment = UIControlContentVerticalAlignmentT
//内容容器的偏移,让文字向右偏移20像素
button.contentEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
//设置标题的背景颜色
button.titleLabel.backgroundColor = [UIColor purpleColor];
//标题颜色
[button setTitleColor:[UIColor yellowColor]forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectF
[self.view addSubview:button];
[button release];
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致

我要回帖

更多关于 uibutton标题颜色 的文章

 

随机推荐