开发者

How to know the displayed text in UILabel?

开发者 https://www.devze.com 2023-01-12 10:15 出处:网络
I have an UIView containing two UILabels, in order to display a string. The first UILabel has a fixed size, and if the string is too long and can\'t hold in this UILabel, I want to display the maximum

I have an UIView containing two UILabels, in order to display a string. The first UILabel has a fixed size, and if the string is too long and can't hold in this UILabel, I want to display the maximum characters I can in the first UILabel, and display the rest of the string in the second UILabel.

But to make this, I must know the exact part of the string displayed in the first UILabel, which is not easy because of the randomness of the string and the linebreaks.

开发者_StackOverflow社区

So, is there a way to get just the text displayed in the first UILabel, without the truncated part of the string?


if ([_infoMedia.description length] > 270) {
        NSRange labelLimit = [_infoMedia.description rangeOfString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(270, (_infoMedia.description.length - 270))];
        _descTop.text = [_infoMedia.description substringToIndex:labelLimit.location];
        _descBottom.text = [_infoMedia.description substringFromIndex:(labelLimit.location+1)];
} else {
            _descTop.text = _infoMedia.description;
            _descBottom.text = @"";
}

Okay that's a late answer but maybe it could help someone. The code above is approximatively the solution I used in my app.

_descTop is my first label and _descBottom is the second label. 270 is a constant equivalent to a little less than the average maximum number of characters displayed in my first label, _descTop. I calculated it by hand, trying with many different strings, maybe there's a better way to do that but this worked not bad.

If the string I want to display (_infoMedia.description) is larger than 270 characters, I isolate the first 270 characters plus the end of the next word in the string (by searching the next space), in the case where the 270 characters limit would cut the string in the middle of a word. Then I put the first part of the string in my first label, and the second part in the second label.

If not, I only put the globality of the string in the first label.

I know that's a crappy solution, but it worked and I didn't found any better way to do that.


Following code might help you in getting what you want!!

//If you want the string displayed in any given rect, use the following code..
@implementation NSString (displayedString)

//font- font of the text to be displayed
//size - Size in which we are displaying the text

-(NSString *) displayedString:(CGSize)size font:(UIFont *)font
{
NSString *written = @"";

int i = 0;
int currentWidth = 0;
NSString *nextSetOfString = @"";

while (1)
{
    NSRange range;
    range.location = i;
    range.length = 1;

    NSString *nextChar = [self substringWithRange:range];
    nextSetOfString = [nextSetOfString stringByAppendingString:nextChar];

    CGSize requiredSize = [nextSetOfString sizeWithFont:font constrainedToSize:CGSizeMake(NSIntegerMax, NSIntegerMax)];
    currentWidth = requiredSize.width;

    if(size.width >= currentWidth && size.height >= requiredSize.height)
    {
        written = [written stringByAppendingString:nextChar];
    }
    else
    {
        break;
    }
    i++;
}


    return written;
}

@end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号