uibutton点击改变图片一点击,文字就变白色,怎么解决

55047人阅读
iOS(437)
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];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1281966次
积分:11920
积分:11920
排名:第1208名
原创:133篇
转载:512篇
评论:48条
(1)(4)(2)(4)(3)(6)(11)(3)(1)(1)(3)(2)(5)(1)(3)(6)(4)(16)(7)(13)(5)(15)(38)(17)(3)(2)(13)(15)(3)(2)(17)(40)(8)(29)(13)(2)(6)(5)(31)(124)(63)(98)给UIButton增加下划线,改变某个范围文字的颜色 - 简书
给UIButton增加下划线,改变某个范围文字的颜色
通过NSMutableAttributedString修改某个范围内的属性在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。自己在网上找了一下,看到部分大神使用NSMutableAttributedString来修改,我也就去了解了一下,下面是部分属性(网上找到的)1.实例化方法和使用方法
实例化方法:
//使用字符串初始化
- (id)initWithString:(NSString *)
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"还没有账号,去注册"];
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)
字典中存放一些属性名和属性值,如:
NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:15.0],NSFontAttributeName,
[UIColor redColor],NSForegroundColorAttributeName,
NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"还没有账号,去注册" attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)
//使用NSAttributedString初始化,跟NSMutableString,NSString类似
使用方法:
//为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)
//为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)
//为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)
//移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)
2.常见的属性及说明
NSFontAttributeName
NSParagraphStyleAttributeName
NSForegroundColorAttributeName
NSBackgroundColorAttributeName
NSStrikethroughStyleAttributeName
删除线格式
NSUnderlineStyleAttributeName
下划线格式
NSStrokeColorAttributeName
删除线颜色
NSStrokeWidthAttributeName
删除线宽度
NSShadowAttributeName
更多方法和属性说明详见苹果官方说明文档:
通过 NSMutableAttributedString 实现富文本
NSString * oneStr = @"还没有账号,去注册";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",oneStr]];
//修改某个范围内的字体大小
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:16.0] range:NSMakeRange(7,2)];
//修改某个范围内字的颜色
[str addAttribute:NSForegroundColorAttributeName value:GLColor(62, 190, 219, 1)
range:NSMakeRange(7,2)];
//在某个范围内增加下划线
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [str length])];
UIButton * registerUsers = [UIButton buttonWithType:UIButtonTypeCustom];
registerUsers.frame = CGRectMake(self.view.centerX - 75, SCREENHEIGHT - 64 -50 - 20, 150, 50);
registerUsers.titleLabel.textAlignment = NSTextAlignmentC
registerUsers.titleLabel.font = [UIFont systemFontOfSize:12];
[registerUsers setAttributedTitle:str forState:UIControlStateNormal];
[registerUsers addTarget:self action:@selector(setregisterUsers:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:registerUsers];
运行效果:
实例运行效果.png
这只是简单的纪录一下,以后会继续填充(对了,暂时发现可以用于 UITextField 和 UILabel 上)ios(106)
1.普通的按钮中的字更改颜色的方法:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];11
这段代码将按钮中的文字的颜色,在一般情况下改成黑色。
2.常见错误:
[customButton.titleLabel setTextColor:[UIColor blackColor]];
customButton.titleLabel.textColor = [UIColor blackColor];
1234512345
通过以上两种方法更改文字颜色无效。&
原因:参考Apple Documentation and API Reference发现&
即titleLabel是readonly,但又由于它继承自UILabel,所以虽然有textcolor的getter和setter方法,但执行无效。&
同理,buttonName.titleLabel setTextColor等类似的getter和setter方法同样无效(titleLabel is readonly)。如需修改,请直接调用buttonName的对应方法(自行查阅Documentation and API Reference)
3.知识拓展&
button有一个readonly attribute叫currentTitle,继承自NSString其功能等同于button.titleLabel.text。作用是提供了对button.titleLabel.text的快速访问。
4.关于setTitleColor方法中的forState参数:&
此参数表示在某种特定的state下,button会使用给定的title,如果button处于某种特定的state,而开发者没有指定此state下的title,系统会默认调用UIControlStateNormal下的title,如果UIControlStateNormal下也没有设置title,则会默认使用system value。
下面给出一个例子,表现不同的state的不同作用。
[presentButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];1212
[presentButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];1212
原理:代码2中,设置presentButton只有在selected的状态下,颜色才会变为橙色,所以点击下一个按钮之后,由于前一个按钮状态变为unselected,所以自然就会变为系统默认的黑色了。免去了多余的若干行代码。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:366742次
积分:4601
积分:4601
排名:第6276名
原创:395篇
转载:388篇
评论:45条
(3)(35)(58)(41)(22)(20)(5)(5)(39)(9)(6)(249)(117)(66)(13)(42)(2)(1)(6)(19)(6)(19)[ios]为什么做 UIButton 的标题标签一路向右移动设置内容的内边距,将它移动到底部的时候?
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我试图使 UIButton 与图像在文字上方。为此,我尝试设置该按钮的边缘嵌入那样:
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 10, 0.0f)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(10, 0.0, 0.0, 0.0)];
得到了什么是的按钮的图像是在同一个地方,该文本是扁老远地跑向一侧,在空间中大约一个字符宽度。
所以我在文字上方图像如何可以简单地设置插入量?那是真的那么多事情要问的吗?
解决方法 1:
你只是错误地计算 edgeInSets.They 有不是你觉得他们的方式。这里是我的测试代码。我确实花了一段时间来把图像和标题放在正确的地方。
UIButton *tmp_testBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 60, 40)];
tmp_testBtn.backgroundColor = [UIColor grayColor];
[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
[tmp_testBtn setImage:[UIImage imageNamed:@"France"] forState:UIControlStateNormal];
[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, -258, 0, 0)];
[tmp_testBtn setTitle:@"testbutton" forState:UIControlStateNormal];
CGRect contentRect = [tmp_testBtn contentRectForBounds:tmp_testBtn.bounds];
NSLog(@"contenRect:%f,%f,%f,%f",contentRect.origin.x,contentRect.origin.y,contentRect.size.width,contentRect.size.height);
CGRect titleRect = [tmp_testBtn titleRectForContentRect:contentRect];
NSLog(@"titleRect:%f,%f,%f,%f",titleRect.origin.x,titleRect.origin.y,titleRect.size.width,titleRect.size.height);
CGRect imageRect = [tmp_testBtn imageRectForContentRect:contentRect];
NSLog(@"imageRect:%f,%f,%f,%f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.size.height);
[self.view addSubview:tmp_testBtn];
[tmp_testBtn release];
顺便说一句,我不会做这样的。我喜欢自定义按钮与 UIImageView 和 UILabel 加上。

我要回帖

更多关于 uibutton 文字颜色 的文章

 

随机推荐