iOS學習之SKTagView的使用

來源:互聯網
上載者:User

標籤:配置   cin   remove   jtag   color   sign   ini   ati   else   

SKTagView是一款支援自動布局的標籤tag.
特性:
-流式展示標籤
-可以配置標籤的顏色、事件、間隔、外邊距等
-支援Auto layout
-可以在UITableViewCell中良好展示
-支援橫豎屏切換
-不使用UICollectionView.

// 配置

- (void)configTagView{    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 100, 30)];    self.label.textColor = [UIColor blackColor];    self.label.font = [UIFont systemFontOfSize:13];    self.label.text = @"曆史搜尋";    [self.view addSubview:self.label];         // 先移除掉所有    [self.tagView removeAllTags];    // 初始化    self.tagView = [[SKTagView alloc] init];    // 整個tagView對應其SuperView的上左下右距離    self.tagView.padding = UIEdgeInsetsMake(10, 10, 10, 10);    // 上下行之間的距離    self.tagView.lineSpacing = 10;    // item之間的距離    self.tagView.interitemSpacing = 20;    // 最大寬度    self.tagView.preferredMaxLayoutWidth = 375;//    @property (assign, nonatomic) CGFloat regularWidth; //!< 固定寬度//    @property (nonatomic,assign ) CGFloat regularHeight; //!< 固定高度    // 原作者沒有能加固定寬度的,自己修改源碼加上了固定寬度和高度,預設是0,就是標籤式布局,如果實現了,那麼就是固定寬度高度//    self.tagView.regularWidth = 100;//    self.tagView.regularHeight = 30;    // 開始載入    [self.dataSource enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {       // 初始化標籤        SKTag *tag = [[SKTag alloc] initWithText:self.dataSource[idx]];        // 標籤相對於自己容器的上左下右的距離        tag.padding = UIEdgeInsetsMake(3, 15, 3, 15);        // 弧度        tag.cornerRadius = 3.0f;        // 字型        tag.font = [UIFont boldSystemFontOfSize:12];        // 邊框寬度        tag.borderWidth = 0;        // 背景        tag.bgColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];        // 邊框顏色        tag.borderColor = [UIColor colorWithRed:191/255.0 green:191/255.0 blue:191/255.0 alpha:1];        // 字型顏色        tag.textColor = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:53/255.0 alpha:1];        // 是否可點擊        tag.enable = YES;        // 加入到tagView        [self.tagView addTag:tag];    }];    // 點擊事件回調    self.tagView.didTapTagAtIndex = ^(NSUInteger idx){                 NSLog(@"點擊了第%ld個",idx);             };         // 擷取剛才加入所有tag之後的內在高度    CGFloat tagHeight = self.tagView.intrinsicContentSize.height;    NSLog(@"高度%lf",tagHeight);    // 根據已經得到的內在高度給SKTagView建立frame    self.tagView.frame = CGRectMake(0, 120, 375, tagHeight);    [self.tagView layoutSubviews];    [self.view addSubview:self.tagView];}

 

 

在UISearchBar的代理方法裡面實現搜尋的時候隱藏,不搜尋的時候顯示

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{    NSLog(@"%@",searchText);    if (searchText.length == 0) {        // 沒有文字了        self.label.hidden = NO;        self.tagView.hidden = NO;    }    else    {        self.label.hidden = YES;        self.tagView.hidden = YES;    }}

 

 

 

下面咱們來看看如何讓他在TableViewCell裡面實現高度自適應的

- (void)configCell:(MKJTagViewTableViewCell *)cell indexpath:(NSIndexPath *)indexpath{    [cell.tagView removeAllTags];    cell.tagView.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;    cell.tagView.padding = UIEdgeInsetsMake(20, 20, 20, 20);    cell.tagView.lineSpacing = 20;    cell.tagView.interitemSpacing = 30;    cell.tagView.singleLine = NO;    // 給出兩個欄位,如果給的是0,那麼就是變化的,如果給的不是0,那麼就是固定的        cell.tagView.regularWidth = 80;        cell.tagView.regularHeight = 30;    NSArray *arr = [self.dataSource[indexpath.row] valueForKey:@"first"];         [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {                 SKTag *tag = [[SKTag alloc] initWithText:arr[idx]];                 tag.font = [UIFont systemFontOfSize:12];        tag.textColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0  alpha:1];        tag.bgColor =[UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0  alpha:1];        tag.cornerRadius = 5;        tag.enable = YES;        tag.padding = UIEdgeInsetsMake(5, 10, 5, 10);        [cell.tagView addTag:tag];    }];         cell.tagView.didTapTagAtIndex = ^(NSUInteger index)    {        NSLog(@"點擊了%ld",index);    };     } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return [tableView fd_heightForCellWithIdentifier:identyfy configuration:^(id cell) {                [self configCell:cell indexpath:indexPath];    }];}

 

iOS學習之SKTagView的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.