关于UIButton使用setAttributedTitle:forState:之后无法改变标题颜色

zhixuruying_0624 2016-08-25 05:24:14
我想要实现点击button之后改变标题颜色的功能,button的标题要求上下两行显示并且font大小不相同,所以自定义了button并使用了NSMutableAttributedString实现这个功能。

但是使用[btn setAttributedTitle:attrString forState:UIControlStateNormal];方法初始化button后
再使用[btn setTitleColor:[UIColor colorWithRed:0.988235 green:0.901961 blue:0.901961 alpha:1.0] forState:UIControlStateNormal];无法改变button标题的颜色

但是打印btn.currentTitleColor的数值已经是显示btn.currentTitleColor:UIDeviceRGBColorSpace 0.988235 0.901961 0.901961 1​(这个数值正是我想要点击之后显示的颜色效果)

这到底是什么情况呢??titleColor的数值已经改变却还是显示原来的颜色。求助大神帮解决这个问题
...全文
657 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
看_灰机 2016-09-06
  • 打赏
  • 举报
回复
那设置选中状态的颜色呢?比如说点击的时候,找到btn上的label,点击btn的时候,把他的text颜色换了 UILabel *label = (UILabel *)[self.view viewWithTag:201]; label.textColor = [UIColor redColor];
zhixuruying_0624 2016-08-25
  • 打赏
  • 举报
回复
// 选中按钮 - (void)clickSegment:(id)sender { CustomBtn *btn = (CustomBtn *)sender; NSUInteger index = [_segmentArray indexOfObject:btn]; [self.myDelagte buttonClick:index]; [self updateSegmentState:index]; }
zhixuruying_0624 2016-08-25
  • 打赏
  • 举报
回复
附上部分代码:

@implementation CustomBtn

- (void)layoutSubviews {
[super layoutSubviews];
self.titleLabel.numberOfLines = 0;
self.titleLabel.textAlignment = NSTextAlignmentCenter;

if ([self.titleLabel.text hasSuffix:@"已开抢"]||[self.titleLabel.text hasSuffix:@"正在疯抢"]) {
NSString *topText = [self.titleLabel.text substringToIndex:6];
NSString *bottomText = [self.titleLabel.text substringFromIndex:6];
NSDictionary *attrDictTop = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
NSDictionary *attrDictbottom = @{NSFontAttributeName : [UIFont systemFontOfSize:10]};

NSAttributedString *attrStrTop = [[NSAttributedString alloc] initWithString:[topText substringWithRange:NSMakeRange(0, topText.length)] attributes:attrDictTop];
NSAttributedString *attrStrBottom = [[NSAttributedString alloc] initWithString:[bottomText substringWithRange:NSMakeRange(0, bottomText.length)] attributes:attrDictbottom];

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithAttributedString:attrStrTop];
[attrString appendAttributedString:attrStrBottom];
[self setAttributedTitle:attrString forState:UIControlStateNormal];
} else {

NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:16]};

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:[self.titleLabel.text substringWithRange:NSMakeRange(0, self.titleLabel.text.length)] attributes:attrDict];
[self setAttributedTitle:attrStr forState:UIControlStateNormal];
}

}

@end


@implementation CustomSegmentView

- (id)initWithFrame:(CGRect)frame segmentArray:(NSArray *)array background:(UIColor *)bkColor selectedColor:(UIColor *)selColor textColor:(UIColor *)textColor {

if (self = [super initWithFrame:frame]) {
[self setBackgroundColor:bkColor];
self.cusBkColor = bkColor;
self.cusSelColor = selColor;
self.cusTextColor = textColor;

self.segmentArray = [[NSMutableArray alloc] init];
self.cursorArray = [[NSMutableArray alloc] init];

float width = frame.size.width * 1.0 / array.count;
for (int i = 0; i < [array count]; i++) {
CustomBtn *btn = [CustomBtn buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(width * i, 0, width, frame.size.height)];

NSString *title = [array objectAtIndex:i];
[btn setTitle:[self addNewLine:title] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:145/255.0 green:141/255.0 blue:140/255.0 alpha:1.0] forState:UIControlStateNormal];

[btn addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventTouchUpInside];

[_segmentArray addObject:btn];
[self addSubview:btn];

CallOutContentView *contentView = [[CallOutContentView alloc]initWithFrame:CGRectMake(btn.bounds.size.width * i, btn.frame.size.height, btn.bounds.size.width, kArrorHeight)];
[_cursorArray addObject:contentView];
[btn.superview addSubview:contentView];

}
for (CustomBtn *btn in _segmentArray) {
if ([btn.titleLabel.text hasSuffix:@"正在疯抢"]) {
[self clickSegment:btn];
}
}
}

return self;
}

// 更新背景颜色跟标题颜色
- (void)updateSegmentState:(NSUInteger)index {

for (int i = 0; i < [_segmentArray count]; i++) {
if (i == index) {
CustomBtn *btn = [_segmentArray objectAtIndex:index];
[btn setBackgroundColor:_cusSelColor];
// 选中时字体颜色
[btn setTitleColor:_cusTextColor forState:UIControlStateNormal];

CallOutContentView *contentView = [_cursorArray objectAtIndex:index];
contentView.selColor = _cusSelColor;
[contentView setNeedsDisplay];

}
else {
CustomBtn *btn = [_segmentArray objectAtIndex:i];
[btn setBackgroundColor:_cusBkColor];
// 未选中按钮字体颜色
[btn setTitleColor:[UIColor colorWithRed:145/255.0 green:141/255.0 blue:140/255.0 alpha:1.0] forState:UIControlStateNormal];

CallOutContentView *contentView = [_cursorArray objectAtIndex:i];
contentView.selColor = [UIColor clearColor];
[contentView setNeedsDisplay];
}
}
}

@end

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧