簡體   English   中英

確定UILabel中截斷的字符數

[英]Determine how many characters in UILabel has truncated

我有一個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有多行時,它會包含斷行符

任何想法來解決它。 任何幫助或建議將非常感謝。

您正在檢查寬度,但您需要檢查高度。

查看這個開源,看看TTTAttributedLabel如何使用CoreText知道何時添加“...”字符。

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

請看drawframesetter:attributesstring:textrange:inrect:context:方法

您還可以更改開源並公開“...”的位置

你可以這樣做,

- (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而label可以顯示hell然后結果應該是7但你會在標簽中看到類似h...因為標簽在截斷時顯示三個點。 所以,如果你想計算完全沒有顯示的字符,那么你需要在你的最終輸出中加3 ,即

  numberOfCharactersThatTruncated = numberOfCharactersThatTruncated + 3;

更新:

我的標簽框架是:(27,75,93,56)

我剛剛設置了top,leading,fixed height,fixed width約束。

Ans相同的代碼就像魅力一樣。

我認為你在計算角色時犯了錯誤。 白色空間應視為一個字符。 然后,如果您的標簽顯示三個點,這意味着標簽可以顯示這三個字符。

例如:你的字符串是:你好嗎?

所以總的性格是:12

現在例如你的標簽顯示:如何......

這意味着它顯示8個字符(包括。(點))

因此, numberOfCharactersThatTruncated將返回4。

如果您正在考慮how a只有可見字符而不考慮. 那么你應該為numberOfCharactersThatTruncated添加3

如果您只想顯示符合標簽的字符,請使用label1.text = [text substringToIndex:numberOfCharsInLabel1]; 這個。

它不會顯示點。

干杯....:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM