文字瀑布實(shí)現(xiàn)

0.png
思路:計(jì)算出每個(gè)格子需要的長(zhǎng)度,然后對(duì)比這一行剩下的長(zhǎng)度夠不夠放;夠的話放這一行,并且更新剩下的長(zhǎng)度;不夠的話重啟一行,更新新的一行剩下的長(zhǎng)度,并且增加高度
缺點(diǎn):當(dāng)一個(gè)格子需要顯示的文字比較多的時(shí)候,會(huì)出現(xiàn)一行放不下,或者上一行空很多的情況,但是需求是每個(gè)格子顯示的文字是4-6個(gè)漢字,所以沒有考慮文字很多的情況

考慮過使用UICollectionView ,但之前寫過一個(gè)類似的,就想用別的方法寫一個(gè)

.h文件
#import <UIKit/UIKit.h>

@interface HuCourseDetailCommitEvaluateView : UIView

@property (nonatomic, copy) blk_NSSteing confirmClick; //確定按鈕回掉

- (instancetype)initWithFrame:(CGRect)frame evaliateArray:(NSArray *)evaliateArray;

- (void)show;

@end


.m文件
#import "HuCourseDetailCommitEvaluateView.h"

@interface HuCourseDetailCommitEvaluateView()

@property (nonatomic, copy) NSString *sendCode;

@end

@implementation HuCourseDetailCommitEvaluateView

- (instancetype)initWithFrame:(CGRect)frame evaliateArray:(NSArray *)evaliateArray{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.frame = CGRectMake(0, 0, HHBWIDTH, HHBHEIGHT);
        UIView * blackBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, HHBWIDTH, HHBHEIGHT)];
        blackBgView.backgroundColor = [UIColor blackColor];
        blackBgView.alpha = 0.33;
        [self addSubview:blackBgView];
        
        UIView *whiteBgView = [[UIView alloc] init];
        whiteBgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:whiteBgView];
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(HHBWIDTH-35, 5, 30, 30)];
        [cancelBtn setImage:IMG(@"close") forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [whiteBgView addSubview:cancelBtn];
        
        float now_xPoint = 5;
        float now_yPoint = 45;
        float edge_width = 10;
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0],NSFontAttributeName, nil];
        
        for (int i=0; i<evaliateArray.count; i++) {
            HuDiscoverCourseEvaluateModel *model = evaliateArray[i];
            float width = [UIKitTool caculateWidth:model.value sizeOfHeight:30 withAttributes:dic]+20;
            
            UIButton *selectBtn = [[UIButton alloc] init];
            selectBtn.tag = 1000+i;
            if ((now_xPoint + edge_width + width) < (HHBWIDTH-15)) {
                selectBtn.frame = CGRectMake(now_xPoint+edge_width, now_yPoint, width, 30);
                now_xPoint += width +edge_width;
            }else{
                now_xPoint = 5;
                now_yPoint += 45;
                selectBtn.frame = CGRectMake(now_xPoint+edge_width, now_yPoint, width, 30);
                now_xPoint += width +edge_width;
            }
            [selectBtn setTitle:model.value forState:UIControlStateNormal];
            selectBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
            selectBtn.layer.masksToBounds = YES;
            selectBtn.layer.borderWidth = 0.5;
            selectBtn.layer.borderColor = [HuConfigration uiColorFromString:@"#DDDDDD"].CGColor;
            [selectBtn setTitleColor:[HuConfigration uiColorFromString:@"#555555"] forState:UIControlStateNormal];
            [selectBtn setTitleColor:[HuConfigration uiColorFromString:@"#FFFFFF"] forState:UIControlStateSelected];
            [selectBtn setBackgroundColor:[HuConfigration uiColorFromString:@"#FFFFFF"] forState:UIControlStateNormal];
            [selectBtn setBackgroundColor:[HuConfigration uiColorFromString:@"#F67B2A"] forState:UIControlStateSelected];
            
            [selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [whiteBgView addSubview:selectBtn];
            
        }
        
        now_yPoint += 45;
        UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, now_yPoint, HHBWIDTH-30, 43)];
        [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
        submitBtn.backgroundColor = mainColor;
        submitBtn.layer.masksToBounds = YES;
        submitBtn.layer.cornerRadius = 2.0;
        [submitBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [whiteBgView addSubview:submitBtn];
        
        whiteBgView.frame = CGRectMake(0, HHBHEIGHT-now_yPoint-60, HHBWIDTH, now_yPoint+60);
        
        _sendCode = @"";
    }
    return self;
}

- (void)selectBtnClick:(UIButton*)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        _sendCode = [NSString stringWithFormat:@"%@%ld",_sendCode,(long)btn.tag];
    }else{
        _sendCode = [_sendCode stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%ld",(long)btn.tag] withString:@""];
    }
}

- (void)confirmBtnClick:(UIButton*)btn{
    self.confirmClick(_sendCode);
    [self dismiss];
}

- (void)cancelBtnClick:(UIButton*)btn{
    [self dismiss];
}


- (void)dismiss
{
    [self removeFromSuperview];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

@end

1.png

自定義UITableViewCell
覺得這樣子實(shí)現(xiàn),不是很靠譜,求大神們指點(diǎn)

.h文件
@interface HuDiscoverCourseEvaluateCell : UITableViewCell

-(void)initArray:(NSArray *)evaluateArray;

@end


.m文件
@implementation HuDiscoverCourseEvaluateCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

-(void)initArray:(NSArray *)evaluateArray{
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    float now_xPoint = 5;
    float now_yPoint = 5;
    float edge_width = 10;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14.0],NSFontAttributeName, nil];
    
    for (int i=0; i<evaluateArray.count; i++) {
        HuDiscoverCourseEvaluateModel *model = evaluateArray[i];
        NSString *showStr = [NSString stringWithFormat:@"%@ (%@)",model.content,model.nums];
        float width = [UIKitTool caculateWidth:showStr sizeOfHeight:30 withAttributes:dic]+20;
        
        UIButton *selectBtn = [[UIButton alloc] init];
        selectBtn.tag = 1000+i;
        if ((now_xPoint + edge_width + width) < (HHBWIDTH-15)) {
            selectBtn.frame = CGRectMake(now_xPoint+edge_width, now_yPoint, width, 30);
            now_xPoint += width +edge_width;
        }else{
            now_xPoint = 5;
            now_yPoint += 45;
            selectBtn.frame = CGRectMake(now_xPoint+edge_width, now_yPoint, width, 30);
            now_xPoint += width +edge_width;
        }
        [selectBtn setTitle:showStr forState:UIControlStateNormal];
        selectBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [selectBtn setTitleColor:[HuConfigration uiColorFromString:@"#F67B2A"] forState:UIControlStateNormal];
        [selectBtn setBackgroundColor:[HuConfigration uiColorFromString:@"#FFF0E5"] forState:UIControlStateNormal];
        
        [self.contentView addSubview:selectBtn];
    }
}

@end


最后編輯于
?著作權(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)容