自动调整 UILabel 的高度 [英] Automatically adjusting height of a UILabel

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

问题描述

我使用以下两种方法(一种是NSString的类别,另一种是UILabel的类别)来自动调整标签的高度里面的文字.大多数情况下它运行良好,但它产生了一些不可预测的结果.我不太确定问题可能出在哪里,我希望你们中的一些好人可以提供帮助.首先,这里是有问题的方法:

NSString 类别:

- (CGFloat) fontSizeWithFont: (UIFont *) font constrainedToSize: (CGSize) size minimumFontSize: (CGFloat) minimumFontSize{CGFloat fontSize = [字体点大小];CGFloat height = [self sizeWithFont: font constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;UIFont *newFont = 字体;//过大时减小字体大小,如果没有高度则中断(空字符串)while (height > size.height && height != 0 && fontSize > minimumFontSize) {字体大小 - ;newFont = [UIFont fontWithName: font.fontName size: fontSize];height = [self sizeWithFont: newFont constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;};//遍历字符串中的单词并调整大小以适合for (NSString *word in [self componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]) {CGFloat width = [word sizeWithFont: newFont].width;while (width > size.width && width != 0 && fontSize > minimumFontSize) {字体大小 - ;newFont = [UIFont fontWithName: font.fontName size: fontSize];width = [word sizeWithFont: newFont].width;}}返回字体大小;}

UILabel 类别:

- (void) sizeToFitMultipleLines{如果(self.adjustsFontSizeToFitWidth){CGFloat AdjustedFontSize = [self.text fontSizeWithFont: self.font constrainedToSize: self.frame.size minimumFontSize: self.minimumScaleFactor];self.font = [self.font fontWithSize:adjustedFontSize];}[自己的sizeToFit];}

出现的问题是标签的高度在其顶部和底部有空白空间,并且其中的字符串越长,空白空间就越大.单行标签在标签框架内的上下可能有 10 个像素,但六行字符串可能有近 100 个像素.我无法在上述方法中找到这些额外空间的来源.

其次,有时会调整标签的宽度,而我只想更改高度.我猜测 [self sizeToFit] 是导致这个特定问题的原因,但我不完全确定,因为如果我删除它,高度仍然未调整.我已经解决了这个宽度问题,方法是在调整标签大小以适应后手动重新定位标签(它的 X 坐标现在是屏幕宽度减去标签宽度,再除以 2).>

有什么想法吗?如果您需要其他信息或代码,请询问.我总是在对相关标签调用 [sizeToFitMultipleLines] 方法之前设置字体,所以这不是问题.

解决方案

我已经尝试过这个并且它对我有用.希望对你有所帮助.

-(void) getDynamicHeightWithString:(NSString*)labelText forWidth:(CGFloat)lblWidth forPoint:(CGPoint)point{//创建标签UILabel *label = [[UILabel alloc] init];label.text = labelText;label.numberOfLines = 0;label.lineBreakMode = NSLineBreakByWordWrapping;//根据字符串设置frameCGSize size = [label.text sizeWithFont:label.font受约束的大小:CGSizeMake(lblWidth,MAXFLOAT)lineBreakMode:UILineBreakModeWordWrap];[label setFrame:CGRectMake(point.x, point.y, size.width, size.height)];//在当前视图中添加标签[self.view addSubview:label];}

使用

NSString *labelText = @"我们的思想造就了我们标签在其顶部和底部有空白空间,并且其中的字符串越长,空白空间就越大.单行标签可能在标签框架内的上下可能有 10 个像素,但六行字符串可能有近 100 个像素.我无法在上述方法中找到这些额外空间的来源.";[self getDynamicHeightWithString:labelText forWidth:200 forPoint:CGPointMake(15, 15)];

I'm using the following two methods (one is a category of NSString and the other a category of UILabel) to automatically adjust the height of a label based on the text inside it. It's working fine for the mostpart, but it's having some unpredictable results. I'm not quite sure where the issue may be occuring and I'm hoping some of you fine folks can offer a helping hand. First off, here are the methods in question:

NSString Category:

- (CGFloat) fontSizeWithFont: (UIFont *) font constrainedToSize: (CGSize) size minimumFontSize: (CGFloat) minimumFontSize
{
    CGFloat fontSize = [font pointSize];
    CGFloat height = [self sizeWithFont: font constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
    UIFont *newFont = font;

    // Reduce font size while too large, break if no height (empty string)
    while (height > size.height && height != 0 && fontSize > minimumFontSize) {
        fontSize--;
        newFont = [UIFont fontWithName: font.fontName size: fontSize];
        height = [self sizeWithFont: newFont constrainedToSize: CGSizeMake(size.width, FLT_MAX) lineBreakMode: NSLineBreakByWordWrapping].height;
    };

    // Loop through words in string and resize to fit
    for (NSString *word in [self componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]) {
        CGFloat width = [word sizeWithFont: newFont].width;
        while (width > size.width && width != 0 && fontSize > minimumFontSize) {
            fontSize--;
            newFont = [UIFont fontWithName: font.fontName size: fontSize];
            width = [word sizeWithFont: newFont].width;
        }
    }
    return fontSize;
}

UILabel Category:

- (void) sizeToFitMultipleLines
{
    if (self.adjustsFontSizeToFitWidth) {
        CGFloat adjustedFontSize = [self.text fontSizeWithFont: self.font constrainedToSize: self.frame.size minimumFontSize: self.minimumScaleFactor];
        self.font = [self.font fontWithSize: adjustedFontSize];
    }
    [self sizeToFit];
}

The issue that's occurring is that the height of the label has empty space at its top and bottom, and that the longer the string inside it is, the larger that empty space is. A single line label might have perhaps 10 pixels above and below it inside the label's frame, but a six-line string may have almost 100 pixels. I'm not able to track down where this extra space is coming from in the methods above.

Secondly, the width of the label is sometimes adjusted where I only want the height to be changed. I'm guessing that [self sizeToFit] is what's causing this particular issue, but am not entirely sure since the height remains unadjusted if I remove it. EDIT: I've fixed this width issue by manually re-positioning the label after its been sized to fit (its X coordinate is now the screen width minus the label width, divided by 2).

Any thoughts? If you need additional info or code, please ask. I'm always setting the font before I call the [sizeToFitMultipleLines] method on the labels in question, so that's not the issue.

解决方案

I have tried this and its working for me. Hope this can help you a bit.

-(void) getDynamicHeightWithString:(NSString*)labelText forWidth:(CGFloat)lblWidth forPoint:(CGPoint)point
{
    //Create Label
    UILabel *label = [[UILabel alloc] init];
    label.text = labelText;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;

    //Set frame according to string
    CGSize size = [label.text sizeWithFont:label.font
                         constrainedToSize:CGSizeMake(lblWidth, MAXFLOAT)
                             lineBreakMode:UILineBreakModeWordWrap];
    [label setFrame:CGRectMake(point.x , point.y , size.width , size.height)];

    //Add Label in current view
    [self.view addSubview:label];    

}

USE

NSString *labelText = @"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far.The issue that's occurring is that the height of the label has empty space at its top and bottom, and that the longer the string inside it is, the larger that empty space is. A single line label might have perhaps 10 pixels above and below it inside the label's frame, but a six-line string may have almost 100 pixels. I'm not able to track down where this extra space is coming from in the methods above.";

[self getDynamicHeightWithString:labelText forWidth:200 forPoint:CGPointMake(15, 15)]; 

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

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