如何动态计算UILabel高度? [英] How to calculate UILabel height dynamically?

查看:121
本文介绍了如何动态计算UILabel高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从给定文本中动态计算UILabel的行数和高度。

解决方案

试试这个

  // UILabel * myLabel; 

CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:myLabel.frame.size
lineBreakMode:NSLineBreakByWordWrapping];

CGFloat labelHeight = labelSize.height;


int lines = [myLabel.text sizeWithFont:myLabel.font
constrainedToSize:myLabel.frame.size
lineBreakMode:NSLineBreakByWordWrapping] .height / 16;
//'16'是字体大小

  int lines = labelHeight / 16; 

NSLog(@行数:%i \ n \ n,行);

  int lines = [myLabel.text sizeWithFont:myLabel.font 
constrainedToSize:myLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap] .height /myLabel.font.pointSize; //从字体中获取字体大小

通过使用类别,只需创建名为

UILabel + UILabelDynamicHeight.h



UILabel + UILabelDynamicHeight.m



高度计算不再紧张。请查看以下实施方案。



iOS7及更新版的更新上图,iOS 7以下:动态计算UILabel高度

  #define SYSTEM_VERSION_EQUAL_TO(v)([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#定义SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch]!= NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch ] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch]!= NSOrderedDescending)
#define iOS7_0 @7.0

UILabel + UILabelDynamicHeight.h

  #import< UIKit / UIKit.h> 
@interface UILabel(UILabelDynamicHeight)

#pragma mark - 计算多行标签的大小
/ * =============== ================================================== === * /

/ *计算多行标签的大小* /

/ * =============== ================================================== === * /
/ **
*返回标签的大小
*
* @param aLabel用于计算高度
*
* @return标签尺寸
* /
- (CGSize)sizeOfMultiLineLabel;

@end

UILabel + UILabelDynamicHeight.m

  #importUILabel + UILabelDynamicHeight.h
@implementation UILabel(UILabelDynamicHeight)


#pragma mark - 计算多行标签的大小,边界,框架
/ * ====================== ============================================== * /

/ *计算多行标签的大小,边界,框架* /

/ * ================== ================================================== * /
/ **
*返回标签的大小
*
* @param aLabel用于计算高度
*
*标签的@return大小$ ​​b $ b * /
- (CGSize)sizeOfMultiLineLabel {

//标签文本
NSString * aLabelTextString = [self text];

//标签字体
UIFont * aLabelFont = [self font];

//标签的宽度
CGFloat aLabelSizeWidth = self.frame.size.width;


if(SYSTEM_VERSION_LESS_THAN(iOS7_0)){
// version< 7.0

return [aLabelTextString sizeWithFont:aLabelFont
constrainedToSize:CGSizeMake(aLabelSizeWidth,MAXFLOAT)
lineBreakMode:NSLineBreakByWordWrapping];
}
else if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(iOS7_0)){
//版本> = 7.0

//返回标签的计算大小
return [aLabelTextString boundingRectWithSize:CGSizeMake(aLabelSizeWidth,MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@ {
NSFontAttributeName:aLabelFont
}
context:nil] .size;

}

return [self bounds] .size;

}
@end


I want to calculate number of lines and height of UILabel dynamically from given text for same.

解决方案

Try this

// UILabel *myLabel;

CGSize labelSize = [myLabel.text sizeWithFont:myLabel.font 
                            constrainedToSize:myLabel.frame.size 
                                lineBreakMode:NSLineBreakByWordWrapping];

CGFloat labelHeight = labelSize.height;


int lines = [myLabel.text sizeWithFont:myLabel.font 
                     constrainedToSize:myLabel.frame.size 
                         lineBreakMode:NSLineBreakByWordWrapping].height/16; 
             // '16' is font size

or

int lines = labelHeight/16;

NSLog(@"lines count : %i \n\n",lines);  

or

int lines = [myLabel.text sizeWithFont:myLabel.font 
                     constrainedToSize:myLabel.frame.size 
                         lineBreakMode:UILineBreakModeWordWrap].height /myLabel.font.pointSize; //fetching font size from font

By Using Categories, Just Create the category class named as

UILabel+UILabelDynamicHeight.h

UILabel+UILabelDynamicHeight.m

No more tension about the height calculation. Please review the below implementation.

Updates for iOS7 & Above,iOS 7 below : Dynamically calculate the UILabel height

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#define iOS7_0 @"7.0"

UILabel+UILabelDynamicHeight.h

#import <UIKit/UIKit.h>
@interface UILabel (UILabelDynamicHeight)

#pragma mark - Calculate the size the Multi line Label
/*====================================================================*/

    /* Calculate the size of the Multi line Label */

/*====================================================================*/
/**
 *  Returns the size of the Label
 *
 *  @param aLabel To be used to calculte the height
 *
 *  @return size of the Label
 */
 -(CGSize)sizeOfMultiLineLabel;

@end

UILabel+UILabelDynamicHeight.m

#import "UILabel+UILabelDynamicHeight.h"
@implementation UILabel (UILabelDynamicHeight)


#pragma mark - Calculate the size,bounds,frame of the Multi line Label
/*====================================================================*/

/* Calculate the size,bounds,frame of the Multi line Label */

/*====================================================================*/
/**
 *  Returns the size of the Label
 *
 *  @param aLabel To be used to calculte the height
 *
 *  @return size of the Label
 */
-(CGSize)sizeOfMultiLineLabel{

    //Label text
    NSString *aLabelTextString = [self text];

    //Label font
    UIFont *aLabelFont = [self font];

    //Width of the Label
    CGFloat aLabelSizeWidth = self.frame.size.width;


    if (SYSTEM_VERSION_LESS_THAN(iOS7_0)) {
        //version < 7.0

        return [aLabelTextString sizeWithFont:aLabelFont
                            constrainedToSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT)
                                lineBreakMode:NSLineBreakByWordWrapping];
    }
    else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(iOS7_0)) {
        //version >= 7.0

        //Return the calculated size of the Label
        return [aLabelTextString boundingRectWithSize:CGSizeMake(aLabelSizeWidth, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{
                                                        NSFontAttributeName : aLabelFont
                                                        }
                                              context:nil].size;

    }

    return [self bounds].size;

}
@end

这篇关于如何动态计算UILabel高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆