用label实现横向瀑布流的方法

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

  - ( 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

转载于:https://www.cnblogs.com/crazygeek/p/5635943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值