Calculate the range of visible text in UILabel

后端 未结 4 2211
鱼传尺愫
鱼传尺愫 2020-12-11 20:43

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 ho

4条回答
  •  心在旅途
    2020-12-11 20:54

    This one works for me, hope it helps you too,

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

提交回复
热议问题