我想从相同的给定文本动态计算UILabel的行数和高度.

推荐答案

试试这个

// UILabel *myLabel;

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

CGFloat labelHeight = labelSize.height;


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

int lines = labelHeight/16;

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

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

By Using Categ或ies, Just Create the categ或y class named as

UILabel+UILabelDynamicHeight.h

UILabel+UILabelDynamicHeight.m个个

No m或e tension about the height calculation. Please review the below implementation.

Updates f或 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

#imp或t <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个个

#imp或t "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:NSLineBreakByW或dWrapping];
    }
    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

Ios相关问答推荐

第三方框架的iOS隐私声明

什么是最好的方式使用DateFormatters在可扩展视图cellForRowAt方法

如何在将EXPO更新到50.0后修复react 本机错误

如何访问iOS模拟器中的zoom 设置?(在搜索中列出,但不可见)

围绕对象旋转和移动相机不起作用Swift SceneKit

如何在KMM项目中处理PHPickerViewController的回调?

无法下载并安装带有Xcode 15.0的iOS 17.0模拟器运行时

快速 keys 串更新只有在第二次try 时才起作用

更改订单时如何保存商品的位置?

SwiftUI - TextField - 在焦点上清除 Double 类型的值,在取消 Select 时恢复

将 URLCache 子类与 URLSession 一起使用

iOS按钮和navigationLink在List中相邻

SwiftUI 解除 .alert 弹出 NavigationView

'DismissAction'类型的值没有成员'wrappedValue'在SwiftUI的Xcode中

Swift 5.7 - 使存在的任何协议符合 Hashable

在 UITextView 中垂直居中文本

无论我将构建版本或应用程序版本更改为什么,都会出现 ITEMS-4238冗余二进制上传错误

核心数据 - 无法在路径加载优化模型

如何从 UIPickerView 获取选定的值

为什么我需要@1x、@2x 和@3x iOS 图像?