简体   繁体   English

调整文本大小以适合大小时,如何删除UILabel末尾的“…”?

[英]How do I remove the “…” at end of UILabel when resizing text to fit?

I am looking to autosize the text and display it all on one line. 我正在寻找自动调整文本大小并将其全部显示在一行上的功能。 My problem is I don't want the "..." appended at the end of my text. 我的问题是我不希望在文本末尾添加“ ...”。 How can I get rid of this? 我该如何摆脱呢? My first image is where it shows the "..." that I want to get rid of. 我的第一个图像是显示要删除的“ ...”的位置。 The second image shows what happens when I change the numberOfLines on the cityLabel from 0 to 1. I also don't want this because I don't want multiple lines (just all on one line). 第二张图显示了将cityLabel上的numberOfLines从0更改为1时发生的情况。我也不希望这样,因为我不需要多行(仅一行就行)。

Here is the code: 这是代码:

UIView *view = [[UIView alloc] initWithFrame: CGRectMake (20, 0, self.view.frame.size.width-20, 90)];
UILabel *cityLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, self.view.frame.size.width-20, 55)];
UILabel *supportedCitiesLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 65, self.view.frame.size.width-20, 20)];

cityLabel.font = [UIFont boldSystemFontOfSize:50.0];
cityLabel.text = @"Dallas Dallas Dallas Dallas Dallas Dallas";
supportedCitiesLabel.text = @"Valley Test";


CGRect labelRect = cityLabel.frame;
cityLabel.adjustsFontSizeToFitWidth = NO;
cityLabel.numberOfLines = 1;


CGFloat fontSize = 50;
while(fontSize > 0.0)
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];

    CGSize size = [cityLabel.text boundingRectWithSize:CGSizeMake(labelRect.size.width, 10000)
                                                                options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                                             attributes:attrDict context:nil].size;

    if(size.height <= labelRect.size.height)
        break;

    fontSize -= 1.0;
}

cityLabel.font = [UIFont boldSystemFontOfSize:fontSize];

Image 1... 图片1 ... 图片1

Image 2... 图片2 ... 图片2

Set [UILabel lineBreakMode] . 设置[UILabel lineBreakMode]

Reference : 参考

Constants 常数

NSLineBreakByWordWrapping

Wrapping occurs at word boundaries, unless the word itself doesn't fit on a single line. 包装发生在单词边界,除非单词本身不适合一行。 See Characters and Grapheme Clusters in String Programming Guide for a discussion of issues related to determining word boundaries. 有关与确定单词边界有关的问题的讨论,请参见《字符串编程指南》中的字符和字素簇。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

NSLineBreakByCharWrapping

Wrapping occurs before the first character that doesn't fit. 包装发生在第一个不合适的字符之前。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

NSLineBreakByClipping

Lines are simply not drawn past the edge of the text container. 线条根本不会越过文本容器的边缘。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

NSLineBreakByTruncatingHead

The line is displayed so that the end fits in the container and the missing text at the beginning of the line is indicated by an ellipsis glyph. 将显示该行,以使其末端适合容器,并且在行首缺少的文本由省略号字形指示。 Although this mode works for multiline text, it is more often used for single line text. 尽管此模式适用于多行文本,但更常用于单行文本。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

NSLineBreakByTruncatingTail

The line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph. 将显示该行,以便使开头适合于容器,并且在行尾缺少的文本由省略号字形指示。 Although this mode works for multiline text, it is more often used for single line text. 尽管此模式适用于多行文本,但更常用于单行文本。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

NSLineBreakByTruncatingMiddle

The line is displayed so that the beginning and end fit in the container and the missing text in the middle is indicated by an ellipsis glyph. 将显示该行,以使开始和结束位置适合容器,而中间的缺失文本用省略号字形指示。 Although this mode works for multiline text, it is more often used for single line text. 尽管此模式适用于多行文本,但更常用于单行文本。

Available in iOS 6.0 and later. 在iOS 6.0及更高版本中可用。

if you want text to autosize to fit width, why did you set cityLabel.adjustsFontSizeToFitWidth = NO; 如果要使文本自动调整大小以适合宽度,为什么要设置cityLabel.adjustsFontSizeToFitWidth = NO; I think it should be set to YES. 我认为应该将其设置为“是”。

Then set cityLabel.lineBreakMode = NSLineBreakByTruncatingTail 然后设置cityLabel.lineBreakMode = NSLineBreakByTruncatingTail

Try it with the flowing. 顺其自然地尝试一下。

 UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 150, self.view.frame.size.width-20, 90)];
UILabel *cityLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 5, self.view.frame.size.width-20, 55)];
UILabel *supportedCitiesLabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 65, self.view.frame.size.width-20, 20)];

cityLabel.font = [UIFont boldSystemFontOfSize:50.0];
cityLabel.text = @"Dallas Dallas Dallas Dallas Dallas Dallas";
supportedCitiesLabel.text = @"Valley Test";


CGRect labelRect = cityLabel.frame;
cityLabel.adjustsFontSizeToFitWidth = NO;
cityLabel.lineBreakMode = NSLineBreakByClipping;
cityLabel.numberOfLines = 1;


CGFloat fontSize = 50;
while(fontSize > 0.0)
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByClipping;
    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:fontSize], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];

    CGSize size = [cityLabel.text boundingRectWithSize:CGSizeMake(labelRect.size.width, 10000)
                                               options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
                                            attributes:attrDict context:nil].size;

    if(size.height <= labelRect.size.height)
        break;

    fontSize -= 1.0;
}

cityLabel.font = [UIFont boldSystemFontOfSize:fontSize];


// [self.view addSubview:view];
// [view addSubview:cityLabel];
// [view addSubview:supportedCitiesLabel];

Some interesting options here but this can all be done inside of Interface Builder with one setting: 这里有一些有趣的选项,但是都可以通过一种设置在Interface Builder内部完成:

在此处输入图片说明

  1. Select the label 选择标签
  2. Under the Attributes Inspector select Minimum Font Scale under the Autoshrink option. 在“属性”检查器下,选择“自动收缩”选项下的“最小字体比例”。
  3. Choose an appropriate scale. 选择合适的比例。

Also, if you have the correct AutoLayout constraints this should work dynamically. 另外,如果您具有正确的自动版式约束,则它应该可以动态工作。

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

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