QMUI_iOS
QMUI_iOS copied to clipboard
UIButton+QMUI.h qmui_setTitleAttributes 无效
Bug 表现 1、设置了多个state的attributes, qbt_titleAttributes中有保存到该attributes 2、多个state的title是一样的, qbt_syncTitleByStates 方法中只会调用 Normal 状态下的 setAttributedTitle, 而其他状态下的attributes实际没有生效
预期的表现 同一个title可以有不同的状态,应用不同的attributes
修改建议 ` // 如果 normal 用了 attributedTitle,那么其他的 state 都必须用 attributedTitle,否则就无法展示出来
- (void)qbt_syncTitleByStates { if (!self.qbt_titleAttributes.count) return; NSMutableDictionary *qbt_titleAttributes = self.qbt_titleAttributes.mutableCopy; // 遍历所有状态下的title, 可能会有同一个title,不同attributes的情况 for (NSNumber *stateValue in self.qbt_statesWithTitle) { UIControlState state = stateValue.unsignedIntegerValue; NSString *title = [self titleForState:state]; NSDictionary<NSAttributedStringKey, id> *attributes = qbt_titleAttributes[stateValue] ?: qbt_titleAttributes[@(UIControlStateNormal)]; if (state != UIControlStateNormal) { [qbt_titleAttributes removeObjectForKey:stateValue]; } NSAttributedString *string = [[NSAttributedString alloc] initWithString:title attributes:attributes]; string = [UIButton qbt_attributedStringByRemovingLastKern:string]; [self setAttributedTitle:string forState:state]; } // 删除默认状态的attributes, 如果还有其他状态的attributes, 再遍历一下赋值 [qbt_titleAttributes removeObjectForKey:@(UIControlStateNormal)]; for (NSNumber *stateValue in qbt_titleAttributes.allKeys) { UIControlState state = stateValue.unsignedIntegerValue; NSString *title = [self titleForState:state]; NSAttributedString *string = [[NSAttributedString alloc] initWithString:title attributes:qbt_titleAttributes[stateValue]]; string = [UIButton qbt_attributedStringByRemovingLastKern:string]; [self setAttributedTitle:string forState:state]; } }`