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

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

代码多谢我家黑妹的帮助

用label实现自适应宽度的方法的更多相关文章

  1. storyboard xib下label怎么自适应宽度高度

    先看需求:两个Label,要求蓝色的label紧跟在红色的label文字后面  ok首选正常添加约束 红色的Label添加宽度,高度,左边,上边约束 蓝色的Label添加宽度,高度,左边,和红色的水平 ...

  2. iOSstoryboard xib下label怎么自适应宽度高度

    先看需求:两个Label,要求蓝色的label紧跟在红色的label文字后面 ok首选正常添加约束 红色的Label添加宽度,高度,左边,上边约束 蓝色的Label添加宽度,高度,左边,和红色的水平对 ...

  3. css布局:左边固定宽度,右边自适应宽度或右侧固定,左侧自适应三种方法

    方法一:浮动布局 这种方法我采用的是左边浮动,右边加上一个margin-left值,让他实现左边固定,右边自适应的布局效果 HTML Markup <div id="left" ...

  4. 从三栏自适应宽度布局到css布局的讨论

    如何实现一个三栏自适应布局,左右各100px,中间随着浏览器宽度自适应? 第一个想到的是使用table布局,设置table的宽度为100%,三个td,第1个和第3个固定宽度为100px,那么中间那个就 ...

  5. css自适应宽度高级写法:一行多列~~~某些列的宽度是固定值

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxgAAACdCAIAAAC4iknFAAAgAElEQVR4nO2d3VsTV7uH51/wqhKotn

  6. JS实现自适应宽度的Tag切换

    效果体验:http://hovertree.com/texiao/js/3.htm 该效果使用纯JavaScript代码,实现TAB页切换效果,TAB标签根据内容自适应宽度,点击TAB标签切换内容页. ...

  7. 根据显示的字符多少来做Label的自适应高度

    根据显示的字符多少来做Label的自适应高度 UILabel *label = [[UILabel alloc]init]; NSString *string = @"其实,经年过往,每个人 ...

  8. [Winform]DataGridView列自适应宽度

    引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...

  9. 关于jqGrid组件表格无法自适应宽度问题

    今天生成了一个4列的表格,但是无法自适应宽度,使用 $(window).resize(function(){ $(window).unbind("onresize"); $(&qu ...

随机推荐

  1. PHP 安装 redis、memcached、openssl、pdo_mysql等

        PHP openssl 扩展的安装 这些插件可以通过在 php.ini 中添加 extension 的方式来加载所需要的插件,其实在 php 的安装包里就已经有相关的插件代码包了,在 php7 ...

  2. php中DateTime的format格式以及 TtoDatetime函数

    Definition and Usage The date() function formats a local time/date. Syntaxdate(format,timestamp)Para ...

  3. tp框架实现ajax注册验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. mysql 安装错误 解决方法

    错误及警告信息:TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_time ...

  5. 【转】101个MySQL调试和优化技巧

    MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些思 ...

  6. 长平狐 Cocos2d-x 的“HelloWorld” 深入分析

                              Cocos2d-x 的“HelloWorld” 深入分析 本节所用Cocos2d-x版本:cocos2d-1.0.1-x-0.12.0 不能免俗,一 ...

  7. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  8. *CCF 201612-2 工资计算(80)

    试题编号:201612-2 试题名称:工资计算 时间限制:1.0s 内存限制:256.0MB 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假设他一个月的税前工资 ...

  9. eclipse 如何debug jdk源码(转)

    转:http://blog.csdn.net/cherrycheng_/article/details/51004386 原英文地址:http://stackoverflow.com/question ...

  10. Android实现渐显按钮的左右滑动效果

    本示例演示在Android中实现带渐显按钮的左右滑动效果. 关于滑动效果,在我的上一篇博文中提到过,有兴趣的朋友可以访问: http://www.cnblogs.com/hanyonglu/archi ...