简体   繁体   English

UILabel截断文本不起作用

[英]UILabel truncate text not working

I set the numberOfLines to 1 in the IB, but when I set the text to a long string, it doesn't truncate. 我在IB中将numberOfLines设置为1,但是当我将文本设置为长字符串时,它不会截断。 If I set the numberOfLines to 2, the truncate works fine.What should I do to truncate a long string into a single line? 如果我将numberOfLines设置为2,则truncate工作正常。我该怎么做才能将长字符串截断为一行?

simple, set the following properties: 简单,设置以下属性:

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;

If using auto layout, in my case, there was a constraint missing. 如果使用自动布局,在我的情况下,缺少约束。 The UILabel grows its width if no constraint is set on its width/trailing. 如果没有在宽度/尾部设置约束,UILabel会增加其宽度。 Once its width is limited, for instance to its superview, the truncation occurs. 一旦其宽度受限,例如其超视图,就会发生截断。

You probably have a constraint on a label in that section that is making things go haywire. 您可能对该部分中的标签有约束,这使得事情变得混乱。

Double check your constraints or remove them for that label or other controls in that section. 仔细检查您的约束或删除该标签或该部分中的其他控件。

The storyboard option for a label: "Line Breaks:Truncate Tail" will to the work you are looking for. 标签的故事板选项:“换行符:截断尾部”将用于您正在寻找的工作。

If you set the label's autoshrink to "Fixed Font Size" in IB, you will always get a truncatation when the string width beyond the label width. 如果在IB中将标签的自动收缩设置为“固定字体大小”,则当字符串宽度超出标签宽度时,将始终获得截断。 I guess you happened to set that to "Minimum Font Scale" or "Minimum Font Font", which will lead a resizing when the string is too long. 我猜你碰巧把它设置为“Minimum Font Scale”或“Minimum Font Font”,当字符串太长时会导致调整大小。

在此输入图像描述

(Xcode 4.5, other version of Xcode and IB may be different property name) (Xcode 4.5,其他版本的Xcode和IB可能是不同的属性名称)

I make two functions, that will help you to do your work. 我有两个功能,可以帮助你完成你的工作。

Basic: This solution I made for task: 基本:这个我为任务做的解决方案:
"minimize font to my min size of font and then put as much info, as possible, but not bigger then maximum width" “将字体最小化到我的最小字体大小,然后尽可能多地放置信息,但不要大于最大宽度”

takeFineFont... function parameters: takeFineFont ...函数参数:
(UIFont*)font - font of the your label ( titleLabel.font ) (UIFont*)font - 标签的字体( titleLabel.font
(NSString*)string - text in your label ( titleLabel.text ) (NSString*)string - 标签中的文本( titleLabel.text
(CGSize)limitStringSize - limit size. (CGSize)limitStringSize - 限制大小。
limitStringSize.width - width limit of your label (Upper limit) limitStringSize.width - 标签的宽度限制(上限)
limitStringSize.height - height limit of your label (Lower limit)(actually, size of font) limitStringSize.height - 标签的高度限制(下限)(实际上,字体大小)

-(UIFont*)takeFineFontSize:(UIFont*)font
              forText:(NSString*)string
             andLimit:(CGSize)limitStringSize{
    UIFont* resultFont = [UIFont fontWithName:[font fontName] size:[font pointSize]];
    if(limitStringSize.width != 0 && limitStringSize.height != 0){
        CGSize currentSize = [string sizeWithFont:resultFont];

        while(/* change font width with upper bound */
              currentSize.width > limitStringSize.width
              &&
              /* change font height with lower bound */
              currentSize.height > limitStringSize.height){

            /*change height and take new width*/
            currentSize.height -= 1;
            currentSize.width = [string sizeWithFont:[resultFont fontWithSize:currentSize.height]].width;
        }
        resultFont = [resultFont fontWithSize:currentSize.height];
    }
    return resultFont;
}
-(double)takeFineWidthForFont:(UIFont*)font
                    forString:(NSString*)string
                     andLimit:(double)widthLimit{
    return MIN([string sizeWithFont:font].width, widthLimit);
}

Suppose, that you have big string in UILabel* titleLabel 假设您在UILabel* titleLabel有大字符串
And you define somewhere: 你在某处定义:

#define maximumLengthOfYourLabel 300
#define minimumSizeOfFont 14

what you will do now? 你现在要做什么? just do this peace of code: 只是做这个代码的和平:

-(void)updateTitleLabelWithBigText:(NSString*)string{

    /*change text*/
    self.titleLabel.text = string;

    /*take pretty small font*/
    self.titleLabel.font = [self takeFineFontSize:self.titleLabel.font 
                                          forText:self.titleLabel.text 
                                         andLimit:CGSizeMake(maximumLengthOfYourLabel,minimumSizeOfFont)
                                          ];

    /*if your text still big, take minimal width and trunctate it*/
    self.titleLabel.width = [self takeFineWidthForFont:self.titleLabel.font
                                             forString:self.titleLabel.text
                                              andLimit:maximumLengthOfYourLabel];
    self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

如果您正在使用属性字符串并将段落样式设置为属性字符串:请确保传递paragraphStyle.lineBreakMode = .byTruncatingTail

Ensure you are not calling sizeToFit() on your label. 确保您没有在标签上调用sizeToFit() It will override the label and constraint settings. 它将覆盖标签和约束设置。

将numberOfLines设置为0,这将允许您在不截断其宽度的情况下垂直扩展uilabel内容。

Perhaps this method can help you: 也许这种方法可以帮助你:

[myLabel sizeToFit]; [myLabel sizeToFit];

The label won't be truncated but it will adjust the label size to fit in one line. 标签不会被截断,但会调整标签大小以适合一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM