文字瀑布实现

作者: Chase_Eleven | 来源:发表于2017-10-31 11:32 被阅读0次
    0.png
    思路:计算出每个格子需要的长度,然后对比这一行剩下的长度够不够放;够的话放这一行,并且更新剩下的长度;不够的话重启一行,更新新的一行剩下的长度,并且增加高度
    缺点:当一个格子需要显示的文字比较多的时候,会出现一行放不下,或者上一行空很多的情况,但是需求是每个格子显示的文字是4-6个汉字,所以没有考虑文字很多的情况

    考虑过使用UICollectionView ,但之前写过一个类似的,就想用别的方法写一个

    .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
    觉得这样子实现,不是很靠谱,求大神们指点
    .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
    


    相关文章

      网友评论

        本文标题:文字瀑布实现

        本文链接:https://www.haomeiwen.com/subject/vozvpxtx.html