【问题标题】:Dynamic UILabel truncating the text动态 UILabel 截断文本
【发布时间】:2012-07-18 12:38:03
【问题描述】:

我正在使用this answer 中提供的代码来创建动态标签,它在大多数情况下都有效。但是,只要标签文本的长度超过 94 个字符,它就会被截断并添加省略号。

还有一件更奇怪的事情是,如果我在字符串中添加更多字符,它们会显示出来,但最后两行仍然会被截断。

例如。

字符串:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two three.

显示如下:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one tw...

但是当我通过在标签中再次使用相同的句子来加倍字符串时,它会显示更多的文本,但仍然会截断它。

例如。

字符串:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to test 
the dynamic bubble sizing one 
two three.

显示如下:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to tes...

这是我正在使用的代码。

NSString *temp = [NSString stringWithFormat:@"this is a very very long string with lots of words to test the dynamic bubble sizing one two three"];

captionLabel.text = temp;
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [temp sizeWithFont:captionLabel.font 
                                  constrainedToSize:maximumLabelSize 
                                      lineBreakMode:captionLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = captionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
captionLabel.frame = newFrame; 

希望有人有想法,因为这让我摸不着头脑。

编辑

感谢@troolee,使用 captionLabel.frame.size.width 而不是硬编码的 296 修复它,如果他/她选择创建答案,我会将其标记为正确。

【问题讨论】:

  • 如果您将使用captionLabel.frame.size.width 而不是硬编码的296 会怎样。标签的宽度可能与296 不同?
  • captionLabel.frame.size.width 修复了它,将其粘贴为答案,我将其标记为正确。似乎我又一次被一些复制意大利面代码抓住了......
  • 我已经编辑了我的代码,它肯定会对你有所帮助:) 谢谢。
  • 你在哪里设置标签?看起来您在计算大小后为其分配了不同的字体?

标签: ios label uilabel


【解决方案1】:

不要写captionLabel.lineBreakMode,而是写UILineBreakModeWordWrap。它应该可以工作。

【讨论】:

    【解决方案2】:

    我希望 @troolee 现在已经将他的评论作为答案,但由于他没有,我将发布答案并将其标记为正确,以便我可以关闭这个问题。

    使用 captionLabel.frame.size.width 而不是硬编码的 296 修复它。

    【讨论】:

      【解决方案3】:

      尝试以下UILabel 类别。感谢创作者。

      UILabel+VAlign.h

      #import <UIKit/UIKit.h>
      #import <Foundation/Foundation.h>
      
      @interface UILabel (VAlign)
      - (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size;
      @end
      

      UILabel+VAlign.h

      #import "UILabel+VAlign.h"
      
      @implementation UILabel (VAlign)
      
      - (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size
      {
          CGSize textSize = [self.text sizeWithFont:self.font
                                  constrainedToSize:size
                                      lineBreakMode:self.lineBreakMode];
      
          CGRect textRect = CGRectMake(self.frame.origin.x,
                                       self.frame.origin.y,
                                       self.frame.size.width,
                                       textSize.height);
          [self setFrame:textRect];
          [self setNeedsDisplay];
      }
      
      @end
      

      【讨论】:

        【解决方案4】:
         NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel END";
        
                testLabel.text = test;
                testLabel.numberOfLines = 0; //will wrap text in new line
                [testLabel sizeToFit];
        
                [self.view addSubview:testLabel];
        

        这肯定会对你有所帮助。

        谢谢

        【讨论】:

        • 这次我已经编辑了我的代码,它肯定会对你有所帮助:) 谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-17
        相关资源
        最近更新 更多