ios標簽的自定義

先上圖。

屏幕快照 2016-07-06 下午5.42.54.png

對該視圖的封裝

#import <UIKit/UIKit.h>
typedef void(^TagListViewOnTagClick)(NSUInteger index, NSString *tag);

/// 標簽樣式風格
typedef NS_ENUM(NSInteger, TagListViewStyle) {
    /// 標準大小
    TagListViewStyleStd,
    /// 大的
    TagListViewStyleBig,
};

@interface searchTagListView : UIView
+ (CGFloat)heightWithTags:(NSArray<NSString *> *)tags style:(TagListViewStyle)style;

/// 標簽整體內容上間隔。默認0
@property (nonatomic, assign) CGFloat contentTopMargin;

/// 標簽數據
@property (nonatomic, copy) NSArray<NSString *> *tags;

- (instancetype)initWithStyle:(TagListViewStyle)style;

/// 用戶點擊了某個標簽
- (void)onTagClick:(TagListViewOnTagClick)block;
@end

#import "searchTagListView.h"
@interface searchTagListView ()
@property (nonatomic, strong) NSMutableArray<UIButton *> *tagButtonList;

@property (nonatomic, assign) TagListViewStyle style;
@property (nonatomic, copy) TagListViewOnTagClick onTagClickBlock;
@end

static CGFloat const kContentLeftRightMargin = 10;
static CGFloat const kTagHeight_std = 25;
static CGFloat const kTagHeight_big = 30;
static CGFloat const kTagSpacing = 15;
static CGFloat const kButtonTitleLeftRightMargin_std = 10;
static CGFloat const kButtonTitleLeftRightMargin_big = 8;

#define kTagFont_std     [UIFont fanZhengLanTingXHFontWithSize:11]
#define kTagFont_big     [UIFont fanZhengLanTingXHFontWithSize:14]
@implementation searchTagListView
/**
 *  計算標簽視圖需要的高度
 *
 *  @param tags          標簽列表
 *  @param tagItemHandle 處理回調,通知外面這個Tag的顯示信息
 *
 *  @return Tags在UI上的高度。
 */
+ (CGFloat)_heightWithTags:(NSArray<NSString *> *)tags style:(TagListViewStyle)style tagItemHandle:(void(^)(NSUInteger index, NSString *tagName, CGSize tagSize, BOOL needWrap))tagItemHandle {
    __block CGFloat tagsHeight = 0;
    if (tags && (tags.count > 0)) {
        UIFont *font = (style == TagListViewStyleStd ? kTagFont_std : kTagFont_big);
        CGFloat titleLeftRightMargin = (style == TagListViewStyleStd ? kButtonTitleLeftRightMargin_std : kButtonTitleLeftRightMargin_big);
        CGFloat tagHeight = (style == TagListViewStyleStd ? kTagHeight_std : kTagHeight_big);
        tagsHeight += tagHeight;
        
        CGFloat tagsContentWdith = SCREEN_WIDTH - kContentLeftRightMargin * 2;
        __block CGFloat currentRowWidth = tagsContentWdith;
        [tags enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat tagWidth = [obj boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size.width + titleLeftRightMargin * 2 + 3;
            BOOL needWrap = NO;
            if (tagWidth > currentRowWidth && currentRowWidth != tagsContentWdith) {
                // 換行
                tagsHeight += kTagSpacing + tagHeight;
                currentRowWidth = tagsContentWdith;
                needWrap = YES;
            }
            GCBlockInvoke(tagItemHandle, idx, obj, CGSizeMake(MIN(tagWidth, tagsContentWdith), tagHeight), needWrap);
            currentRowWidth -= (tagWidth + kTagSpacing);
        }];
    }
    return tagsHeight;
}

+ (CGFloat)heightWithTags:(NSArray<NSString *> *)tags style:(TagListViewStyle)style {
    return [self _heightWithTags:tags style:style tagItemHandle:nil];
}

- (instancetype)init {
    if (self = [super init]) {
        self.contentTopMargin = 0;
        self.tagButtonList = [NSMutableArray array];
        self.style = TagListViewStyleStd;
    }
    return self;
}

- (instancetype)initWithStyle:(TagListViewStyle)style {
    if (self = [super init]) {
        self.contentTopMargin = 0;
        self.tagButtonList = [NSMutableArray array];
        self.style = style;
    }
    return self;
}

#pragma mark - public methods

- (void)setTags:(NSArray<NSString *> *)tags {
    _tags = [tags copy];
    [self _reloadButtonList];
}

- (void)onTagClick:(TagListViewOnTagClick)block {
    self.onTagClickBlock = block;
}

#pragma mark - private methods

- (UIButton *)_createButtonWithTagName:(NSString *)tagName {
    UIButton *button = [[UIButton alloc] init];
    button.titleLabel.font = (self.style == TagListViewStyleStd ? kTagFont_std : kTagFont_big);
    button.backgroundColor = k_COLOR_E7E7E7;
    CGFloat titleLeftRightMargin = (self.style == TagListViewStyleStd ? kButtonTitleLeftRightMargin_std : kButtonTitleLeftRightMargin_big);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, titleLeftRightMargin, 0, titleLeftRightMargin);
    button.layer.cornerRadius = (self.style == TagListViewStyleStd ? kTagHeight_std : kTagHeight_big) * 0.5;
    button.layer.borderWidth = 0;
    [button setTitle:tagName forState:UIControlStateNormal];
    [button setTitleColor:k_COLOR_949494 forState:UIControlStateNormal];
    _weak(self);
    [button addControlEvents:UIControlEventTouchUpInside action:^(UIControl *control, NSSet *touches) {
        _strong_check(self);
        GCBlockInvoke(self.onTagClickBlock, control.tag, [(UIButton *)control titleForState:UIControlStateNormal]);
    }];
    return button;
}

- (void)_reloadButtonList {
    [self.tagButtonList enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.tagButtonList removeAllObjects];
    
    __block UIView *prevView = nil;
    [searchTagListView _heightWithTags:self.tags style:self.style tagItemHandle:^(NSUInteger index, NSString *tagName, CGSize tagSize, BOOL needWrap) {
        UIButton *btn = [self _createButtonWithTagName:tagName];
        [self addSubview:btn];
        [self.tagButtonList addObject:btn];
        
        UIButton *button = self.tagButtonList[index];
        [button mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (prevView == nil) {
                make.left.equalTo(self).offset(kContentLeftRightMargin);
                make.top.equalTo(self).offset(self.contentTopMargin);
            }
            else if (needWrap) {
                make.left.equalTo(self).offset(kContentLeftRightMargin);
                make.top.equalTo(prevView.mas_bottom).offset(kTagSpacing);
            }
            else {
                make.left.equalTo(prevView.mas_right).offset(kTagSpacing);
                make.top.equalTo(prevView);
            }
            make.size.mas_equalTo(tagSize);
        }];
        prevView = button;
    }];
}

@end

*使用方法

 CGFloat height=[searchTagListView heightWithTags:_tagListView.tags style:TagListViewStyleStd];
    [self.hotTagLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchView.mas_bottom).offset(-12);
        make.left.equalTo(self.view);
        make.width.equalTo(@75);
        make.height.equalTo(@40);
        
    }];

-(searchTagListView *)tagListView{
    if (!_tagListView) {
        _weak(self);
        _tagListView=[[searchTagListView alloc] initWithStyle:TagListViewStyleStd];
     _tagListView.backgroundColor=self.view.backgroundColor;
        
        
        [_tagListView onTagClick:^(NSUInteger index, NSString *tag) {
             _strong_check(self);
            [self.searchTextField resignFirstResponder];
            self.keyword=tag;
           
            [self _loadDataWithIsLatest:YES];
        }];

    }
    return _tagListView;
}

使用方法截自項目,封裝的類中含有項目中定義的宏替換掉就可以了
最后附上封裝類中出現的宏
#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

#define GCBlockInvoke(block, ...)   \
do {                            \
    if (block) {                \
        block(__VA_ARGS__);    \
    }                           \
} while(0)
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define _weak(x)    __weak typeof(x) weak##x = x
#define _strong(x)  typeof(weak##x) x = weak##x
#define _strong_check(x, ...) typeof(weak##x) x = weak##x; if (!weak##x) return __VA_ARGS__;
#define RGB(r, g, b)        [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define k_COLOR_E7E7E7          RGB(221, 225, 224)
#define k_COLOR_949494          RGB(142, 151, 146)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,124評論 25 708
  • 一、const與宏的區別(面試題): const簡介`:之前常用的字符串常量,一般是抽成宏,但是蘋果不推薦我們抽成...
    shuai1234閱讀 204評論 0 2
  • 在確定了自己要減肥這件事情之后,我先開始上網了解了一些健康減肥的知識。 比如,對于健康掉肉的速度應該是多少?運動的...
    來一罐解藥閱讀 387評論 9 1
  • 天冷了,陰郁的天空再加上涼風襲人,真是一個不夠明朗的天氣,不過,究竟是秋風還是臺風,已經難以分辨,只是感覺呼嘯而過...
    Jessy自由行走的貓閱讀 461評論 0 0
  • // //ViewController.m //輪播圖 // //Created by lanou on 16/7...
    BlueLantern閱讀 140評論 0 0