计算UILabel中可见文本的范围

计算UILabel中可见文本的范围

本文介绍了计算UILabel中可见文本的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定大小的 UILabel ,我在此 UILabel 中设置的文字可以是200,5或500个字符长。我想要做的是计算我可以使用当前 UILabel 大小在此 UILabel 中放入多少可见文本。

I have an UILabel with fixed size, the text that I set into this UILabel can be 200, 5 or 500 characters long. What I want to do is to calculate how much visible text can I put into this UILabel with the current UILabel size.

为什么我要这样做?因为我想在文本的末尾添加 ...阅读更多文本,但不是在整个文本的末尾,只是在可见文本的末尾在 UILabel

Why I want to do that? Because I want to add a ...Read more text at the end of the text, but not at the end of the whole text, just at the end of the visible text in the UILabel.

提前致谢。

推荐答案

所以我创建了一个方法,它返回当前可见的字符串高度(具有UITextView / UITextField或UILabel的大小),并且它也支持iOS6 +,这就是我所做的:

So I've created a method which returns the current visible string height (with the size of the UITextView / UITextField or UILabel) and it's also support iOS6+, this is what I did:

- (NSUInteger)fitString:(NSString *)string intoLabel:(UILabel *)label
{
    UIFont *font           = label.font;
    NSLineBreakMode mode   = label.lineBreakMode;

    CGFloat labelWidth     = label.frame.size.width;
    CGFloat labelHeight    = label.frame.size.height;
    CGSize  sizeConstraint = CGSizeMake(labelWidth, CGFLOAT_MAX);

    if (SYSTEM_VERSION_GREATER_THAN(iOS_7))
    {
        NSDictionary *attributes = @{ NSFontAttributeName : font };
        NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes];
        CGRect boundingRect = [attributedText boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        {
            if (boundingRect.size.height > labelHeight)
            {
                NSUInteger index = 0;
                NSUInteger prev;
                NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];

                do
                {
                    prev = index;
                    if (mode == NSLineBreakByCharWrapping)
                        index++;
                    else
                        index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
                }

                while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height <= labelHeight);

                return prev;
            }
        }
    }
    else
    {
        if ([string sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height > labelHeight)
        {
            NSUInteger index = 0;
            NSUInteger prev;
            NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];

            do
            {
                prev = index;
                if (mode == NSLineBreakByCharWrapping)
                    index++;
                else
                    index = [string rangeOfCharacterFromSet:characterSet options:0 range:NSMakeRange(index + 1, [string length] - index - 1)].location;
            }

            while (index != NSNotFound && index < [string length] && [[string substringToIndex:index] sizeWithFont:font constrainedToSize:sizeConstraint lineBreakMode:mode].height <= labelHeight);

            return prev;
        }
    }

    return [string length];
}

当然SYSTEM_VERSION_GREATER_THAN(iOS_7)都是我定义的宏。你也应该定义自己的。

Of course that SYSTEM_VERSION_GREATER_THAN(iOS_7) are both macros that I defined. You also should define your own.

祝你好运!

这篇关于计算UILabel中可见文本的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:33