确定UILabel中截断的字符数

时间:2016-05-27 03:16:32

标签: ios objective-c uilabel

我有UILabel,我想确定UILabel中有多少字符被截断。
使用下面的代码,如果我的UILabel只有1行

,我可以确定
int numberOfTruncatedCharacter = 0;
NSString *text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";

NSArray  *words = [text componentsSeparatedByString:@" "];
NSString *newStr = @"";
for (NSString *word in words) {
    NSString *statement = [NSString stringWithFormat:@"%@ %@",newStr, word];
    CGSize size = [statement sizeWithAttributes:@{NSFontAttributeName: self.aboutLabel.font}];

    if(size.width < self.aboutLabel.bounds.size.width){
        newStr = [NSString stringWithFormat:@"%@ %@",newStr, word];
    }else{
        numberOfTruncatedCharacter++;
    }
    NSLog(@"%@ | %f | %f",word,size.width,self.aboutLabel.bounds.size.width);
}
NSLog(@"number of truncated character = %d",numberOfTruncatedCharacter);

它正常工作,这是日志

2016-05-27 10:02:44.642 EarCrush[2284:42690] Lorem | 32.622070 | 355.000000
2016-05-27 10:02:44.642 EarCrush[2284:42690] Ipsum | 64.345703 | 355.000000
2016-05-27 10:02:44.642 EarCrush[2284:42690] is | 74.243164 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] simply | 107.138672 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] dummy | 145.644531 | 355.000000
2016-05-27 10:02:44.643 EarCrush[2284:42690] text | 165.952148 | 355.000000
2016-05-27 10:02:44.644 EarCrush[2284:42690] of | 177.978516 | 355.000000
2016-05-27 10:02:44.644 EarCrush[2284:42690] the | 195.854492 | 355.000000
2016-05-27 10:02:44.645 EarCrush[2284:42690] printing | 235.004883 | 355.000000
2016-05-27 10:02:44.646 EarCrush[2284:42690] and | 255.429688 | 355.000000
2016-05-27 10:02:44.647 EarCrush[2284:42690] typesetting | 309.921875 | 355.000000
2016-05-27 10:02:44.648 EarCrush[2284:42690] industry. | 353.134766 | 355.000000
2016-05-27 10:02:44.649 EarCrush[2284:42690] Lorem | 385.756836 | 355.000000
2016-05-27 10:02:44.649 EarCrush[2284:42690] Ipsum | 384.858398 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] has | 372.202148 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] been | 379.218750 | 355.000000
2016-05-27 10:02:44.650 EarCrush[2284:42690] the | 371.010742 | 355.000000
2016-05-27 10:02:44.651 EarCrush[2284:42690] industry's | 401.171875 | 355.000000
2016-05-27 10:02:44.651 EarCrush[2284:42690] standard | 397.431641 | 355.000000
2016-05-27 10:02:44.652 EarCrush[2284:42690] dummy | 391.640625 | 355.000000
2016-05-27 10:02:44.652 EarCrush[2284:42690] text | 373.442383 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] ever | 375.844727 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] since | 379.541016 | 355.000000
2016-05-27 10:02:44.653 EarCrush[2284:42690] the | 371.010742 | 355.000000
2016-05-27 10:02:44.654 EarCrush[2284:42690] 1500s | 383.374023 | 355.000000
2016-05-27 10:02:44.654 EarCrush[2284:42690] number of truncated character = 13

问题是当我的UILabel有多行时,我将代码更改为

if(size.width < self.aboutLabel.bounds.size.width * numberOfLines){

然而,它无法正确计算。我想也许当UILabel有多行时,它会包含断行符

任何想法来解决它。任何帮助或建议都将非常感激。

2 个答案:

答案 0 :(得分:1)

您正在检查宽度,但需要检查高度。

查看这个开源,看看TTTAttributedLabel如何使用CoreText知道何时添加“...”字符。

https://github.com/TTTAttributedLabel/TTTAttributedLabel/blob/master/TTTAttributedLabel/TTTAttributedLabel.m

查看drawframesetter:attributesstring:textrange:inrect:context:method

您还可以更改开源并公开“...”的位置

答案 1 :(得分:1)

你可以这样做,

- (void)viewDidLoad {

  [super viewDidLoad];
  UILabel *label1 = self.myLabel;


UIFont *font = label1.font;
NSString *text = @"This is the label text wich will be truncate!!";

CGRect label1Frame = label1.frame;

NSUInteger numberOfCharsInLabel1 = NSNotFound;

NSUInteger numberOfCharactersThatTruncated = NSNotFound;

for (int i = [text length]; i >= 0; i--) {
    NSString *substring = [text substringToIndex:i];
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:substring
                                                                         attributes:@{ NSFontAttributeName : font }];
    CGSize size = CGSizeMake(label1Frame.size.width, CGFLOAT_MAX);
    CGRect textFrame = [attributedText boundingRectWithSize:size
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                    context:nil];

    if (CGRectGetHeight(textFrame) <= CGRectGetHeight(label1Frame)) {
        numberOfCharsInLabel1 = i;

        numberOfCharactersThatTruncated = [text length] - numberOfCharsInLabel1;
        break;
    }
}

if (numberOfCharsInLabel1 == NSNotFound) {
    // TODO: Handle this case.
}

 label1.text = [text substringToIndex:numberOfCharsInLabel1];  // If you want to set character that label only can accept without truncating

//OR


label1.text = text;  //set full text with truncating


NSLog(@"Number of Truncated Characters %d",numberOfCharactersThatTruncated);


// Do any additional setup after loading the view.
}

此处self.myLabel引用标签的出口。

numberOfCharactersThatTruncated是您的desired output

这将返回标签中被截断的字符数。

这将返回完全截断的字符我的意思是如果你的字符串是Hello there并且标签能够显示hell,那么结果应该是7但你会在标签中看到像h...一样,因为标签在截断时显示三个点。因此,如果您想计算完全没有显示的字符,那么您需要在最终输出中添加3

  numberOfCharactersThatTruncated = numberOfCharactersThatTruncated + 3;

<强>更新

我的标签框架为:(27,75,93,56)

我刚刚设置了top,leading,fixed height,fixed width个约束。

Ans相同的代码就像魅力一样。

我认为你在计算角色时犯了错误。白色空间应视为一个字符。然后,如果您的标签显示三个点,这意味着标签可以显示这三个字符。

示例:你的字符串是:你好吗?

所以总的性格是:12

现在例如你的标签显示:如何......

表示它显示8个字符(包括。(点))

因此,numberOfCharactersThatTruncated将返回4.

如果您认为how a只是可见字符而不考虑.,那么您应该将3添加到numberOfCharactersThatTruncated

如果您只想显示符合标签的字符,请使用label1.text = [text substringToIndex:numberOfCharsInLabel1];

它不会显示点。

干杯....:)