对象 - 单词行截断 [英] Obj c - Word Line Truncation

查看:69
本文介绍了对象 - 单词行截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to show my long text in a UILabel. But, My design having small size of frame for that UILabel. So, i want to truncate my long text like this[see below]:

Ex:

Given Text: "I want to show my long text in a UILabel"

Recent Result: [Using lineBreakMode:]
1. I want to s........a UILabel
2. I want to s.....
3. I want to s
 
Excepting Result: "I want to...."

[Note:  I want truncation after the word which can fit within their label frame.]

I hope that you can sense about my excepting result. Am weak in English!.

推荐答案

UILabel有一个属性 lineBreakMode 即可。



提示:Apple文档非常好,所以仔细阅读以了解所有功能。
The UILabel has a property lineBreakMode that is the way to go.

tip: the Apple documentation is very good, so read it carefully to understand all features.


1周后,我自己找到了解决方案。请检查代码的有效性并对其进行评论。



我的解决方案处理您为控制文本标题提供的多行文本。



尝试这种类型的截断!!!



解决方案:

After 1 week, i found solution myself. Please check the efficient of the code and comment it.

My solution handles multiple number of lines of text you given for your control title of text.

Taste this type of truncation!!!

Solution:
-(NSString *)methodForDisplayTextWithTruncate:(UILabel *)lblFinalResult{

    NSLog(@"Old Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
    NSLog(@"Event Length: %i", [lblFinalResult.text length]);

    NSString *strGivenText, *strFuncResult, *stringThatFits, *backupStr;
    int i,checkpoint=lblFinalResult.numberOfLines,bound=0;
    NSArray *arrForGivenText_Words;
    NSMutableArray *mutArrForWords;

    stringThatFits=@"";
    strFuncResult=@"";
    backupStr=@"";
    mutArrForWords=[[NSMutableArray alloc] initWithObjects:nil];

    //Custom Truncate Function
    strGivenText=lblFinalResult.text;
    arrForGivenText_Words = [strGivenText componentsSeparatedByString:@" "];

    for(int y=0; y<[arrForGivenText_Words count];y++){
        if(![[arrForGivenText_Words objectAtIndex:y] isEqualToString:@""]){
            [mutArrForWords addObject:[arrForGivenText_Words objectAtIndex:y]];
        }
    }
//    NSLog(@"Corrected:%@",mutArrForWords);

    for (i=0; i < [mutArrForWords count]; i++)
    {
        /* must follow @" %@" - a space before %@ */
        NSString *tempString = [stringThatFits stringByAppendingFormat:@"%@ ", mutArrForWords[i]];
        NSLog(@"TempString:%@",tempString);

        CGRect boundingRect = [tempString boundingRectWithSize:CGSizeMake(999, 999)                                                       options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:lblFinalResult.font} context:nil];

        if (boundingRect.size.width > lblFinalResult.frame.size.width) {
            NSLog(@"Bound(%i)", bound++);

            if(i==0){
                [lblFinalResult setText:@"..."];
                NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
                return lblFinalResult.text;
            }
            if(checkpoint>1){
                backupStr=@"";
                for (int j = 0 ; j < i; j++){
                    backupStr=[backupStr stringByAppendingFormat:@"%@ ", mutArrForWords[j]];
                }
                NSLog(@"Backup Bound:%@",backupStr);
                stringThatFits=mutArrForWords[i];
                stringThatFits=[stringThatFits stringByAppendingString:@" "];
                checkpoint--;
            }
            else{
                strFuncResult=@"";
                for (int z = 0 ; z < i; z++)
                {
                    strFuncResult = [strFuncResult stringByAppendingFormat:@"%@ ", mutArrForWords[z]];
                }
                strFuncResult = [strFuncResult substringToIndex:strFuncResult.length-(strFuncResult.length>0)];

                lblFinalResult.frame= CGRectMake(lblFinalResult.frame.origin.x, lblFinalResult.frame.origin.y, lblFinalResult.frame.size.width+10, lblFinalResult.frame.size.height);
                strFuncResult=[strFuncResult stringByAppendingString:@"..."];
                [lblFinalResult setText:strFuncResult];
                NSLog(@"Final_1:%@", lblFinalResult.text);
                NSLog(@"Final Length: %i", [lblFinalResult.text length]);
                NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
                return lblFinalResult.text;
            }
        }
        else{
            stringThatFits = tempString;
        }
    }
    stringThatFits=@"";
    for (int z = 0 ; z < i; z++)
    {
        stringThatFits = [stringThatFits stringByAppendingFormat:@"%@ ", mutArrForWords[z]];
    }
    stringThatFits = [stringThatFits substringToIndex:stringThatFits.length-(stringThatFits.length>0)];

    [lblFinalResult setText:stringThatFits];
    NSLog(@"Final_2:%@", lblFinalResult.text);
    NSLog(@"Final Length: %i", [lblFinalResult.text length]);
    NSLog(@"New Frame: %@", NSStringFromCGRect(lblFinalResult.frame));
    return lblFinalResult.text;
}


这篇关于对象 - 单词行截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆