文字瀑布实现

作者: 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


相关文章

  • 文字瀑布实现

    思路:计算出每个格子需要的长度,然后对比这一行剩下的长度够不够放;够的话放这一行,并且更新剩下的长度;不够的话重启...

  • 瀑布流布局 的学习

    1- 实现瀑布流布局效果 瀑布流效果瀑布流代码木桶布局效果木桶布局代码瀑布流布局 2- 实现一个新闻瀑布流新闻...

  • swift实现瀑布流

    类似瀑布流的使用场景很多. 实现瀑布流的关键节点: 实现cell的高度不同且没有规则的展示 这里整理实现瀑布流思路...

  • RecyclerView瀑布流优化方案探讨

    目录介绍 01.规则瀑布流实现 02.不规则瀑布流实现2.1 实现方式2.2 遇到问题 03.瀑布流上拉加载 04...

  • 瀑布流和懒加载结合

    实现一个瀑布流布局效果 瀑布流

  • 「实战」蘑菇街 PC 端首页,瀑布流布局的实现原理与细节技巧

    零、介绍 这篇文章主要是介绍网站页面瀑布流布局的实现,主要包括: 瀑布流是什么 瀑布流的实现原理 瀑布流的使用场景...

  • 瀑布流布局

    题目1:实现一个瀑布流布局效果 jsbin-实现瀑布流布局 题目2:根据课程视频实现一个瀑布流新闻网站,查看效果 ...

  • 瀑布流布局_木桶布局

    题目1: 实现一个瀑布流布局效果 瀑布流 题目2:实现木桶布局效果 木桶布局 题目3:**实现一个新闻瀑布流新闻网...

  • 瀑布流布局

    瀑布流布局 实现一个瀑布流布局效果 预览 根据课程视频实现一个新闻瀑布流新闻网站,查看效果

  • 瀑布流布局

    题目1: 实现一个瀑布流布局效果瀑布流代码题目2:实现木桶布局效果木桶布局代码题目3:实现一个新闻瀑布流新闻网站,...

网友评论

    本文标题:文字瀑布实现

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