對(duì)UIButton 的新的認(rèn)識(shí) -- 標(biāo)簽的實(shí)現(xiàn)

新認(rèn)識(shí)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)識(shí)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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容