首页 > 技术文章 > 用label实现横向瀑布流的方法

crazygeek 2017-02-06 16:49 原文

要实现图中   关键字   的实现可用下面代码

  - (void)initLayout {
    self.keywordLabel = [[UILabel alloc] init];
    self.keywordLabel.textColor = [UIColor grayColor];
    self.keywordLabel.font = [UIFont systemFontOfSize:13];
    self.keywordLabel.text = @"关键字:";
    [self.contentView addSubview:self.keywordLabel];
    [self.keywordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.width.mas_offset(70);
    }];
    
    self.buttonArray = @[@"111", @"123", @"12466788", @"35346", @"111", @"123", @"12466788", @"35346",@"111", @"123", @"12466788", @"35346", @"111", @"123", @"12466788", @"35346"].mutableCopy;
    int j = 1;
    for (int i = 0; i < self.buttonArray.count; i++) {
//        创建button
        UIButton *button = [[UIButton alloc] init];
        [self.arr addObject:button];
//        背景颜色
        button.backgroundColor = [UIColor orangeColor];
//        [button setTitle:self.buttonArray[i] forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        button.titleLabel.font = [UIFont systemFontOfSize:13];
//        计算宽度
        CGFloat width = [SmallTools textWidthWithText:self.buttonArray[i] font:[UIFont systemFontOfSize:13]];
        if (i != 0) {
            CGFloat upWidth = [SmallTools textWidthWithText:self.buttonArray[i - 1] font:[UIFont systemFontOfSize:13]];
            UIButton *arrButton = self.arr[i - 1];
        if (arrButton.frame.origin.x + upWidth + width  + 40 > [UIScreen mainScreen].bounds.size.width) {
                button.frame = CGRectMake(90, j * 40 + 10, width + 10, 25);
                j++;
        }else {
            button.frame = CGRectMake( arrButton.frame.origin.x + upWidth + 20, 40 * (j - 1) + 10, width + 10, 25);
        }
        [self.contentView addSubview:button];
        } else {
            button.frame = CGRectMake(90, 10, width + 10, 25);
            [self.contentView addSubview:button];
        }
    }
    _numberOfButton = j;
}

SmallTools.h

 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface SmallTools : NSObject


// 根据输入尺寸修改图片大小,并返回UIImage
+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;

// 声明类方法用来计算文本高度
+ (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font;

// 声明类方法用来计算图片的高度
+ (CGFloat)imageHeightWithImage:(UIImage *)image;

// 声明类方法用来计算文本宽度
+ (CGFloat)textWidthWithText:(NSString *)text font:(UIFont *)font;

@end

 

SmallTools.m

 #import "SmallTools.h"

@implementation SmallTools

+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


// 计算文本高度
+ (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font {
    
    // iOS7.0中求文本高度的方法,返回一个CGRect的高度
    
    // 第一个参数
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 10000);
    
    // 第二个参数:设置以行高为单位
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil];
    
    return rect.size.height;
}

// 计算图片高度
+ (CGFloat)imageHeightWithImage:(UIImage *)image {
    
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    //    float scile = height / width;
    //
    //    float screenH = [UIScreen mainScreen].bounds.size.width;
    
    
    NSLog(@"%f", width);
    //    return  scile * screenH;
    return height / width * [UIScreen mainScreen].bounds.size.width;
}

// 计算文本高度
+ (CGFloat)textWidthWithText:(NSString *)text font:(UIFont *)font {
    
    // iOS7.0中求文本高度的方法,返回一个CGRect的高度
    
    // 第一个参数
    CGSize size = CGSizeMake(100000, 25);
    
    // 第二个参数:设置以行高为单位
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil];
    
    return rect.size.width;
}


@end

推荐阅读