计算 UILabel 中可见文本的范围

Calculate the range of visible text in UILabel

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

我为什么要这样做?因为我想在文末加一段...Read more文字,但不是在整个文末,只是在UILabel.

中可见文字的末尾

提前致谢。

尝试在 UILabel 中使用以下方法

在字符串中添加您的文本,例如 :-> lbl.text=@"abcdeedjedaekd.....Read more."

这是一个想法,不是正确的解决方案,但可能对您有所帮助

您可以设置固定数量的字符后跟:

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

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

所以我创建了一个方法,它 returns 当前可见字符串高度(与 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)都是我定义的宏。你也应该定义你自己的。

祝你好运!

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

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