计算UILabel中可见文本的范围

时间:2015-08-31 12:27:53

标签: ios objective-c uilabel nsattributedstring

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

为什么我要这样做?因为我想在文本的末尾添加...Read more文本,而不是在整个文本的末尾,只是在UILabel的可见文本的末尾添加。

提前致谢。

4 个答案:

答案 0 :(得分:3)

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

- (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)都是我定义的宏。你也应该定义自己的。

祝你好运!

答案 1 :(得分:0)

尝试在UILabel中使用以下方法

Set Your Text Like this

在字符串中添加文字,如: - &gt; lbl.text = @“abcdeedjedaekd .....阅读更多。”

它的理念不是正确的解决方案,但可能对您有所帮助

答案 2 :(得分:0)

您可以设置固定数量的字符,然后按:

CGSize size = [string sizeWithFont:font 
                 constrainedToSize:sizeConstraint 
                     lineBreakMode:UILineBreakModeWordWrap]; 

根据特定字体的文本长度找出CGSize的{​​{1}}多少并追加。阅读更多固定数量的字符。 这种转变的原因是在截断之前没有找到标签中可见字符数的本地方法。

答案 3 :(得分:0)

这个对我有用,希望它对你也有帮助,

- (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);


    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;
        }
    }


    return [string length];
 }