• iOS开发——文本高度


    1.简单的计算文本高度 

    // 要计算的文本内容
        NSString *testString = @"刘成利,软件工程专业毕业,iOS开发者,目前工作于北京,在证券金融领域从事iOS App开发,由于工作有闲暇时间,所以开始写博客,分享自己浅薄的iOS相关开发经验,希望任何人士多多指正指点或者批评教育。";
        
        UILabel *labeliOS7        = [[UILabel alloc]init];
        labeliOS7.text            = testString;
        labeliOS7.font            = [UIFont systemFontOfSize:17];
        labeliOS7.backgroundColor = [UIColor lightGrayColor];
        labeliOS7.numberOfLines   = 0;
        // 计算高度
        /*
         参数说明:boundingRectWithSize:后面跟随宽度和高度
         */
        CGSize titleSize = [testString boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
        
        labeliOS7.frame  = CGRectMake(20, 300, 300, titleSize.height);
        
        [self.view addSubview:labeliOS7];

    2.计算文本高度的工具类(转自我的领头同事:游贤明教授)

    //
    //  NSString+LabelWidthAndHeight.h
    //  ZiPeiYi
    //
    //  Created by YouXianMing on 15/12/9.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface NSString (LabelWidthAndHeight)
    
    /**
     *  Get the string's height with the fixed width.
     *
     *  @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
     *  @param width     Fixed width.
     *
     *  @return String's height.
     */
    - (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width;
    
    /**
     *  Get the string's width.
     *
     *  @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
     *
     *  @return String's width.
     */
    - (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute;
    
    /**
     *  Get a line of text height.
     *
     *  @param attribute String's attribute, eg. attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]}
     *
     *  @return String's width.
     */
    + (CGFloat)aLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute;
    
    @end
    //
    //  NSString+LabelWidthAndHeight.m
    //  ZiPeiYi
    //
    //  Created by YouXianMing on 15/12/9.
    //  Copyright © 2015年 YouXianMing. All rights reserved.
    //
    
    #import "NSString+LabelWidthAndHeight.h"
    
    @implementation NSString (LabelWidthAndHeight)
    
    - (CGFloat)heightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute fixedWidth:(CGFloat)width {
        
        NSParameterAssert(attribute);
        
        CGFloat height = 0;
        
        if (self.length) {
            
            CGRect rect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                             options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
                           NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil];
            
            height = rect.size.height;
        }
        
        return height;
    }
    
    - (CGFloat)widthWithStringAttribute:(NSDictionary <NSString *, id> *)attribute {
        
        NSParameterAssert(attribute);
        
        CGFloat width = 0;
        
        if (self.length) {
            
            CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)
                                             options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
                           NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil];
            
            width = rect.size.width;
        }
        
        return width;
    }
    
    + (CGFloat)aLineOfTextHeightWithStringAttribute:(NSDictionary <NSString *, id> *)attribute {
        
        CGFloat height = 0;
        CGRect rect    = [@"One" boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                              options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |
                          NSStringDrawingUsesFontLeading
                                           attributes:attribute
                                              context:nil];
        
        height = rect.size.height;
        return height;
    }
    
    @end

  • 相关阅读:
    hdu 1427 速算24点
    HDFS Safemode问题
    surfaceDestroyed什么时候被调用
    C# DataTable的詳細使用方法
    HDU 2460 Network(双连通+树链剖分+线段树)
    撰写架构设计文档的心得体会
    win7下jdk安装环境变量配置
    Android提高第二篇之SurfaceView的基本使用
    UVa 490
    bnu 34982 Beautiful Garden(暴力)
  • 原文地址:https://www.cnblogs.com/LiuChengLi/p/4899289.html
Copyright © 2020-2023  润新知