iPhone:如何理解文本是否适合标签

发布于 2024-12-14 03:06:55 字数 83 浏览 0 评论 0原文

使用 IOS 4,我想以编程方式了解给定文本是否适合某个 UILabel,我可以在不进行复杂计算的情况下获取该信息吗?如果不是,最基本的计算方法是什么?

Using IOS 4, I want to understand programatically if a given text is fitting to a certain UILabel or not, can i get that information without doing complex calculations? if not what is the most basic approach for calculating that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-12-21 03:06:55

您可以在 NSString 上调用 - (CGSize)sizeWithFont:(UIFont *)font

You can call - (CGSize)sizeWithFont:(UIFont *)font on NSString.

迷爱 2024-12-21 03:06:55

NSString 方法 sizeWithFont:contrainedToSize:lineBreakMode: 可以提供帮助。它会给你 CGSize 和计算出的字符串大小。只需将其与您的 UILabel.frame.size 进行比较即可。

NSString method sizeWithFont:contrainedToSize:lineBreakMode: could help. It will give you CGSize with calculated size of string. Just compare this with your UILabel.frame.size.

策马西风 2024-12-21 03:06:55

要确定任何字体的任何标签的高度,您可以使用此函数

+(float)calculateHeightOfTextFromWidth:(NSString*)text:(UIFont*)withFont:(float)width:(UILineBreakMode)lineBreakMode;

+(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width:(UILineBreakMode)lineBreakMode
{
if (([text length]>0))
{
    CGSize suggestedSize = [text sizeWithFont:withFont   constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
    return suggestedSize.height;
}
return 18;
}

这是一个了解此函数工作原理的示例

if (![isRateABusinessController length]) {

     companyNameLabel.text = [NSString stringWithFormat:@"%@",[allDetails objectForKey:@"CNAME"]];

     NSString *cAdd    = [NSString stringWithFormat:@"%@",[allDetails objectForKey:@"CADD"]];

    float height = [ConfirmationViewController calculateHeightOfTextFromWidth:cAdd :[UIFont fontWithName:@"Arial" size:14] :170 :UILineBreakModeWordWrap];

    addressLabel.text = [NSString stringWithFormat:@"%@",cAdd];

    if (height > 30) {
        CGRect rect = CGRectMake(addressLabel.frame.origin.x , addressLabel.frame.origin.y, addressLabel.frame.size.width, addressLabel.frame.size.height+12.0);
        addressLabel.frame = rect;
    }
}
else
{
    companyNameLabel.text = [NSString stringWithFormat:@"%@",[Utility selectedCompanyName]];

    NSString *cAdd    = [NSString stringWithFormat:@"%@",[Utility selectedCompanyFullAddress]];
   // NSString *star     = [NSString stringWithFormat:@"Rating: %@",ratingName];

    float height = [ConfirmationViewController calculateHeightOfTextFromWidth:cAdd :[UIFont fontWithName:@"Arial" size:14] :170 :UILineBreakModeWordWrap];

    addressLabel.text = [NSString stringWithFormat:@"%@",cAdd];

    if (height > 30) {
        CGRect rect = CGRectMake(addressLabel.frame.origin.x , addressLabel.frame.origin.y, addressLabel.frame.size.width, addressLabel.frame.size.height+12.0);

        addressLabel.frame = rect;
    }
}

}

To Determine the height of any lable with any font you can use this function

+(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width: (UILineBreakMode)lineBreakMode;

+(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width:(UILineBreakMode)lineBreakMode
{
if (([text length]>0))
{
    CGSize suggestedSize = [text sizeWithFont:withFont   constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
    return suggestedSize.height;
}
return 18;
}

This is one example to understand the working of this function

if (![isRateABusinessController length]) {

     companyNameLabel.text = [NSString stringWithFormat:@"%@",[allDetails objectForKey:@"CNAME"]];

     NSString *cAdd    = [NSString stringWithFormat:@"%@",[allDetails objectForKey:@"CADD"]];

    float height = [ConfirmationViewController calculateHeightOfTextFromWidth:cAdd :[UIFont fontWithName:@"Arial" size:14] :170 :UILineBreakModeWordWrap];

    addressLabel.text = [NSString stringWithFormat:@"%@",cAdd];

    if (height > 30) {
        CGRect rect = CGRectMake(addressLabel.frame.origin.x , addressLabel.frame.origin.y, addressLabel.frame.size.width, addressLabel.frame.size.height+12.0);
        addressLabel.frame = rect;
    }
}
else
{
    companyNameLabel.text = [NSString stringWithFormat:@"%@",[Utility selectedCompanyName]];

    NSString *cAdd    = [NSString stringWithFormat:@"%@",[Utility selectedCompanyFullAddress]];
   // NSString *star     = [NSString stringWithFormat:@"Rating: %@",ratingName];

    float height = [ConfirmationViewController calculateHeightOfTextFromWidth:cAdd :[UIFont fontWithName:@"Arial" size:14] :170 :UILineBreakModeWordWrap];

    addressLabel.text = [NSString stringWithFormat:@"%@",cAdd];

    if (height > 30) {
        CGRect rect = CGRectMake(addressLabel.frame.origin.x , addressLabel.frame.origin.y, addressLabel.frame.size.width, addressLabel.frame.size.height+12.0);

        addressLabel.frame = rect;
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文