要实现图中 关键字 的实现可用下面代码
- (void)initLayout { self.keywordLabel = [[UILabel alloc] init]; self.keywordLabel.textColor = [UIColor grayColor]; self.keywordLabel.font = [UIFont systemFontOfSize:]; self.keywordLabel.text = @"关键字:"; [self.contentView addSubview:self.keywordLabel]; [self.keywordLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(); make.left.equalTo(self.contentView).offset(); make.width.mas_offset(); }]; self.buttonArray = @["].mutableCopy; ; ; 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:]; // 计算宽度 CGFloat width = [SmallTools textWidthWithText:self.buttonArray[i] font:[UIFont systemFontOfSize:]]; ) { CGFloat upWidth = [SmallTools textWidthWithText:self.buttonArray[i - ] font:[UIFont systemFontOfSize:]]; UIButton *arrButton = self.arr[i - ]; > [UIScreen mainScreen].bounds.size.width) { button.frame = CGRectMake(, j * + , width + , ); j++; }else { button.frame = CGRectMake( arrButton.frame.origin.x + upWidth + , * (j - ) + , width + , ); } [self.contentView addSubview:button]; } else { button.frame = CGRectMake(, , width + , ); [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(, , 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, ); // 第二个参数:设置以行高为单位 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(, ); // 第二个参数:设置以行高为单位 CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil]; return rect.size.width; } @end
代码多谢我家黑妹的帮助