新認(rèn)識1
標(biāo)簽的實(shí)現(xiàn).jpg
- (void)customWith:(LikeModel *)model{
[self.iconImage sd_setImageWithURL:[NSURL URLWithString:model.image]];
self.titleL.text = model.title;
self.desL.text = model.Description;
for (id view in self.tagsView.subviews) {
[view removeFromSuperview];
}
NSInteger i = 0;
CGFloat orginX = 0;
CGFloat btnH = 25;
CGSize btnSize = CGSizeMake(100, 25);
for (TagsInfoModel * tagInfo in model.tagsInfo) {
NSString * text = tagInfo.text;
CGRect rect = [text boundingRectWithSize:btnSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(orginX + 15, 0, rect.size.width + 20, btnH);
btn.tag = 1000 + i;
btn.titleLabel.font = [UIFont systemFontOfSize:12];
orginX += btn.frame.size.width + 15;
i +=1;
btn.layer.cornerRadius = btnH / 2;
btn.layer.masksToBounds = true;
btn.layer.borderColor = [[UIColor orangeColor] CGColor];
btn.layer.borderWidth = 0.8;
[btn setTitle:text forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.tagsView addSubview:btn];
}
新認(rèn)識2
根據(jù)button 上的標(biāo)題字?jǐn)?shù) 來進(jìn)行 選中 和不選中狀態(tài)下不不同表現(xiàn)
第一種方式添加
let name = (self.loveArr![i]["name"] as! String)
// let range = name.startIndex..<name.endIndex
let nameRange:NSRange = NSMakeRange(0, name.characters.count)
let normalAttribute = NSMutableAttributedString.init(string:name)
normalAttribute.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 15),NSForegroundColorAttributeName:UIColor.black], range:nameRange)
let selectedAttribute = NSMutableAttributedString.init(string:name)
selectedAttribute.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 17),NSForegroundColorAttributeName:NavColor], range: nameRange)
button.setTitleColor(UIColor.black, for: UIControlState.highlighted)
button.setAttributedTitle(normalAttribute, for: UIControlState.normal)
button.setAttributedTitle(selectedAttribute, for: UIControlState.selected)
第二種方式添加(建議用)
NSMutableAttributedString * selectedAttributed = [[NSMutableAttributedString alloc]initWithString:self.btnsTitles[i] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25],NSForegroundColorAttributeName:[UIColor orangeColor]}];
[button setAttributedTitle:selectedAttributed forState:UIControlStateSelected]; // 選中狀態(tài)下的 大小 和 顏色
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者