欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

iOS如何實(shí)現(xiàn)動(dòng)態(tài)自適應(yīng)標(biāo)簽

小編這次要給大家分享的是iOS如何實(shí)現(xiàn)動(dòng)態(tài)自適應(yīng)標(biāo)簽,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了監(jiān)利免費(fèi)建站歡迎大家使用!

先上效果圖

iOS如何實(shí)現(xiàn)動(dòng)態(tài)自適應(yīng)標(biāo)簽

 設(shè)計(jì)要求

1、標(biāo)簽的寬度是按內(nèi)容自適應(yīng)的

2、一行顯示的標(biāo)簽個(gè)數(shù)是動(dòng)態(tài)的,放得下就放,放不下就換行

3、默認(rèn)選中第一個(gè)

4、至少選中一個(gè)標(biāo)簽

實(shí)現(xiàn)思路

首先我們從這個(gè)效果上來(lái)看,這個(gè)標(biāo)簽是有選中和不選中狀態(tài),那我們首選的控件肯定就是用 UIButton來(lái)實(shí)現(xiàn)了。

這個(gè)小程度的重點(diǎn)就在于標(biāo)簽?zāi)茏詣?dòng)換行,還是智能的,不是固定一行多少個(gè)那種,這個(gè)我們通過(guò)計(jì)算每個(gè)按鈕實(shí)際寬度與屏幕的寬度進(jìn)行比較就能判斷是否需要換行了。

還有一點(diǎn)就是處理 至少選中一個(gè)標(biāo)簽的功能,我這里有一種方式,就是控制按鈕的 userInteractionEnabled 屬性來(lái)實(shí)現(xiàn),如果只有一個(gè)按鈕的時(shí)候就把那一個(gè)按鈕的這個(gè)屬性給設(shè)置成 NO,這樣就禁止用戶對(duì)它進(jìn)行點(diǎn)擊事件了,這個(gè)時(shí)候其它按鈕是可以正常選中的,只要選中的按鈕大于1個(gè),那就把剛才那個(gè)按鈕屬性再改成YES,這樣那個(gè)按鈕就又能點(diǎn)了。

具體看代碼

創(chuàng)建一個(gè)繼承于UIView的 XGTagView類(lèi)

//
// XGTagView.h
// 動(dòng)態(tài)標(biāo)簽
//
// Created by xgao on 17/3/22.
// Copyright © 2017年 xgao. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface XGTagView : UIView

/**
 * 初始化
 *
 * @param frame frame
 * @param tagArray 標(biāo)簽數(shù)組
 *
 * @return
 */
- (instancetype)initWithFrame:(CGRect)frame tagArray:(NSMutableArray*)tagArray;

// 標(biāo)簽數(shù)組
@property (nonatomic,retain) NSArray* tagArray;

// 選中標(biāo)簽文字顏色
@property (nonatomic,retain) UIColor* textColorSelected;
// 默認(rèn)標(biāo)簽文字顏色
@property (nonatomic,retain) UIColor* textColorNormal;

// 選中標(biāo)簽背景顏色
@property (nonatomic,retain) UIColor* backgroundColorSelected;
// 默認(rèn)標(biāo)簽背景顏色
@property (nonatomic,retain) UIColor* backgroundColorNormal;


@end
//
// XGTagView.m
// 動(dòng)態(tài)標(biāo)簽
//
// Created by xgao on 17/3/22.
// Copyright &copy; 2017年 xgao. All rights reserved.
//

#import "XGTagView.h"

@interface XGTagView()

@end

@implementation XGTagView

/**
 * 初始化
 *
 * @param frame frame
 * @param tagArray 標(biāo)簽數(shù)組
 *
 * @return
 */
- (instancetype)initWithFrame:(CGRect)frame tagArray:(NSArray*)tagArray{
 
 self = [super initWithFrame:frame];
 if (self) {
 _tagArray = tagArray;
 [self setUp];
 }
 return self;
}

// 初始化
- (void)setUp{
 
 // 默認(rèn)顏色
 _textColorNormal = [UIColor darkGrayColor];
 _textColorSelected = [UIColor whiteColor];
 _backgroundColorSelected = [UIColor redColor];
 _backgroundColorNormal = [UIColor whiteColor];
 
 // 創(chuàng)建標(biāo)簽按鈕
 [self createTagButton];
}

// 重寫(xiě)set屬性
- (void)setTagArray:(NSMutableArray *)tagArray{
 
 _tagArray = tagArray;
 
 // 重新創(chuàng)建標(biāo)簽
 [self resetTagButton];
}

- (void)setTextColorSelected:(UIColor *)textColorSelected{

 _textColorSelected = textColorSelected;
 // 重新創(chuàng)建標(biāo)簽
 [self resetTagButton];
}

- (void)setTextColorNormal:(UIColor *)textColorNormal{
 
 _textColorNormal = textColorNormal;
 // 重新創(chuàng)建標(biāo)簽
 [self resetTagButton];
}

- (void)setBackgroundColorSelected:(UIColor *)backgroundColorSelected{
 
 _backgroundColorSelected = backgroundColorSelected;
 // 重新創(chuàng)建標(biāo)簽
 [self resetTagButton];
}

- (void)setBackgroundColorNormal:(UIColor *)backgroundColorNormal{
 
 _backgroundColorNormal = backgroundColorNormal;
 // 重新創(chuàng)建標(biāo)簽
 [self resetTagButton];
}

#pragma mark - Private

// 重新創(chuàng)建標(biāo)簽
- (void)resetTagButton{
 
 // 移除之前的標(biāo)簽
 for (UIButton* btn in self.subviews) {
 [btn removeFromSuperview];
 }
 // 重新創(chuàng)建標(biāo)簽
 [self createTagButton];
}

// 創(chuàng)建標(biāo)簽按鈕
- (void)createTagButton{
 
 // 按鈕高度
 CGFloat btnH = 28;
 // 距離左邊距
 CGFloat leftX = 6;
 // 距離上邊距
 CGFloat topY = 10;
 // 按鈕左右間隙
 CGFloat marginX = 10;
 // 按鈕上下間隙
 CGFloat marginY = 10;
 // 文字左右間隙
 CGFloat fontMargin = 10;
 
 for (int i = 0; i < _tagArray.count; i++) {
 
 UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
 btn.frame = CGRectMake(marginX + leftX, topY, 100, btnH);
 btn.tag = 100+i;
 // 默認(rèn)選中第一個(gè)
 if (i == 0) {
  btn.selected = YES;
 }
 
 // 按鈕文字
 [btn setTitle:_tagArray[i] forState:UIControlStateNormal];
 
 //------ 默認(rèn)樣式
 //按鈕文字默認(rèn)樣式
 NSMutableAttributedString* btnDefaultAttr = [[NSMutableAttributedString alloc]initWithString:btn.titleLabel.text];
 // 文字大小
 [btnDefaultAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, btn.titleLabel.text.length)];
 // 默認(rèn)顏色
 [btnDefaultAttr addAttribute:NSForegroundColorAttributeName value:self.textColorNormal range:NSMakeRange(0, btn.titleLabel.text.length)];
 [btn setAttributedTitle:btnDefaultAttr forState:UIControlStateNormal];
 
 // 默認(rèn)背景顏色
 [btn setBackgroundImage:[self imageWithColor:self.backgroundColorNormal] forState:UIControlStateNormal];
 
 //----- 選中樣式
 // 選中字體顏色
 NSMutableAttributedString* btnSelectedAttr = [[NSMutableAttributedString alloc]initWithString:btn.titleLabel.text];
 // 選中顏色
 [btnSelectedAttr addAttribute:NSForegroundColorAttributeName value:self.textColorSelected range:NSMakeRange(0, btn.titleLabel.text.length)];
 // 選中文字大小
 [btnSelectedAttr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, btn.titleLabel.text.length)];
 [btn setAttributedTitle:btnSelectedAttr forState:UIControlStateSelected];
 
 // 選中背景顏色
 [btn setBackgroundImage:[self imageWithColor:self.backgroundColorSelected] forState:UIControlStateSelected];
 
 // 圓角
 btn.layer.cornerRadius = btn.frame.size.height / 2.f;
 btn.layer.masksToBounds = YES;
 // 邊框
 btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
 btn.layer.borderWidth = 0.5;
 
 // 設(shè)置按鈕的邊距、間隙
 [self setTagButtonMargin:btn fontMargin:fontMargin];
 
 // 處理?yè)Q行
 if (btn.frame.origin.x + btn.frame.size.width + marginX > self.frame.size.width) {
  // 換行
  topY += btnH + marginY;
  
  // 重置
  leftX = 6;
  btn.frame = CGRectMake(marginX + leftX, topY, 100, btnH);
  
  // 設(shè)置按鈕的邊距、間隙
  [self setTagButtonMargin:btn fontMargin:fontMargin];
 }
 
 // 重置高度
 CGRect frame = btn.frame;
 frame.size.height = btnH;
 btn.frame = frame;
 
 //----- 選中事件
 [btn addTarget:self action:@selector(selectdButton:) forControlEvents:UIControlEventTouchUpInside];
 
 [self addSubview:btn];
 
 leftX += btn.frame.size.width + marginX;
 }
 
 // 檢測(cè)按鈕狀態(tài),最少選中一個(gè)
 [self checkButtonState];
}

// 設(shè)置按鈕的邊距、間隙
- (void)setTagButtonMargin:(UIButton*)btn fontMargin:(CGFloat)fontMargin{
 
 // 按鈕自適應(yīng)
 [btn sizeToFit];
 
 // 重新計(jì)算按鈕文字左右間隙
 CGRect frame = btn.frame;
 frame.size.width += fontMargin*2;
 btn.frame = frame;
}

// 檢測(cè)按鈕狀態(tài),最少選中一個(gè)
- (void)checkButtonState{
 
 int selectCount = 0;
 UIButton* selectedBtn = nil;
 for(int i=0;i < _tagArray.count; i++){
 UIButton* btn = (UIButton*)[self viewWithTag:100+i];
 if(btn.selected){
  selectCount++;
  selectedBtn = btn;
 }
 }
 if (selectCount == 1) {
 // 只有一個(gè)就把這一個(gè)給禁用手勢(shì)
 selectedBtn.userInteractionEnabled = NO;
 }else{
 // 解除禁用手勢(shì)
 for(int i=0;i < _tagArray.count; i++){
  UIButton* btn = (UIButton*)[self viewWithTag:100+i];
  if(!btn.userInteractionEnabled){
  btn.userInteractionEnabled = YES;
  }
 }
 }
}

// 根據(jù)顏色生成UIImage
- (UIImage*)imageWithColor:(UIColor*)color{
 
 CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
 // 開(kāi)始畫(huà)圖的上下文
 UIGraphicsBeginImageContext(rect.size);
 
 // 設(shè)置背景顏色
 [color set];
 // 設(shè)置填充區(qū)域
 UIRectFill(CGRectMake(0, 0, rect.size.width, rect.size.height));
 
 // 返回UIImage
 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
 // 結(jié)束上下文
 UIGraphicsEndImageContext();
 return image;
}

#pragma mark - Event

// 標(biāo)簽按鈕點(diǎn)擊事件
- (void)selectdButton:(UIButton*)btn{
 
 btn.selected = !btn.selected;
 
 // 檢測(cè)按鈕狀態(tài),最少選中一個(gè)
 [self checkButtonState];
}


@end

看完這篇關(guān)于iOS如何實(shí)現(xiàn)動(dòng)態(tài)自適應(yīng)標(biāo)簽的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

當(dāng)前名稱:iOS如何實(shí)現(xiàn)動(dòng)態(tài)自適應(yīng)標(biāo)簽
網(wǎng)站URL:http://www.aaarwkj.com/article18/gjcedp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、Google、移動(dòng)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、定制網(wǎng)站虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司
日韩日韩毛片日韩毛片| 国产精品毛片在线看不卡| 久久91亚洲精品中文字幕| 午夜精品视频免费91| 久久久亚洲福利精品午夜| 亚洲国产精品一区二区av| 日韩一级不卡高清视频| 国产又大又长又粗又硬又猛| 亚洲精品欧美无人区乱码| 欧美日韩国产成人激情| 欧美日韩一区二区三区激情| 国产亚洲精品视频在线| 亚洲视一区二区三区四区| 中文字幕中出亚洲精品| 欧美十日本亚洲激情视频| 四季一区二区三区av| 国产一区二区三区不卡视频| 欧美日韩欧美黄色三级| 日本2区3区视频在线观看| 在线一区免费视频播放| 人人妻人人澡人人爽老妇| 免费在线观看性生活视频| 日韩一级精品电影网| 哪里可以看日韩免费毛片| 日韩免费毛片在线观看| 在线看黄视频网站永久免费| 日韩av一区二区人妻| 午夜91激情福利视频| 日韩精品一区二区三区电影在线播放| 欧美一区二区三在线| 国内丰满少妇嗷嗷叫在线播放 | 麻豆映画传媒在线播放| 国产精品va在线观看入口| 中文字幕乱码日韩一二三区| 国产麻豆精品传媒av| 亚洲日本一区二区高清在线 | 麻豆久久精品国产亚洲精品超碰热| 免费成人激情在线电影| 在线激情av中文字幕| 色偷拍亚洲偷自拍二区| 国产美女被狂操到高潮|