我想要实现点击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的数值已经改变却还是显示原来的颜色。求助大神帮解决这个问题
再使用[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的数值已经改变却还是显示原来的颜色。求助大神帮解决这个问题
[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
- (void)clickSegment:(id)sender {
CustomBtn *btn = (CustomBtn *)sender;
NSUInteger index = [_segmentArray indexOfObject:btn];
[self.myDelagte buttonClick:index];
[self updateSegmentState:index];
}
UILabel *label = (UILabel *)[self.view viewWithTag:201];
label.textColor = [UIColor redColor];