Drawing a UILabel according to line length

I draw a shortcut using drawRect, and the code looks something like below.

if (productName && productName.length > 0) {
    UILabel *productNameLabel = [[UILabel alloc]init];
    productNameLabel.numberOfLines = 2;
    productNameLabel.attributedText = [self shadowedTextWithString:productName fontName:@"ProximaNovaA-Light" fontSize:productNameLabelFontSize isOfferType:NO];
    [productNameLabel sizeToFit];
    //drawing the UILabel
    [productNameLabel drawTextInRect:CGRectMake(25, labelYPosition, productNameLabel.frame.size.width, productNameLabel.frame.size.height)];
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 25, labelYPosition);
    [productNameLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -25, -labelYPosition);

    labelYPosition += productNameLabel.frame.origin.y + productNameLabel.frame.size.height+20;
}

However, it productNameLabel.numberOfLiness = 2doesn’t work at all ... If the line has a length that exceeds the width of the screen, the text is truncated, but UILabelremains one liner.

Does anyone know how I can do this, so if the line length exceeds the screen width, the exceeded words will go to the second line?

Thank!

updated code, still not working!

if (productName && productName.length > 0) {
    UILabel *productNameLabel = [[UILabel alloc]init];
    productNameLabel.lineBreakMode = YES;
    productNameLabel.numberOfLines = 0;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    NSMutableAttributedString *productNameAttributedString = [self shadowedTextWithString:productName fontName:@"ProximaNovaA-Light" fontSize:productNameLabelFontSize isOfferType:NO];

    [productNameAttributedString addAttribute:NSParagraphStyleAttributeName
                 value:style
                 range:NSMakeRange(0, productNameAttributedString.length)];

    productNameLabel.attributedText = productNameAttributedString;


    CGSize constrainedSize = CGSizeMake(paramImageView.image.size.width -50  , 9999);

    CGRect requiredHeight = [productNameLabel.attributedText boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    if (requiredHeight.size.width > productNameLabel.frame.size.width) {
        requiredHeight = CGRectMake(25,labelYPosition, productNameLabel.frame.size.width, requiredHeight.size.height);
    }

    CGRect newFrame = productNameLabel.frame;
    newFrame.size.height = requiredHeight.size.height;
    productNameLabel.frame = newFrame;

    productNameLabel.backgroundColor = [UIColor redColor];

    [productNameLabel drawTextInRect:CGRectMake(25, labelYPosition, paramImageView.image.size.width-50, requiredHeight.size.height)];

    //[productNameLabel drawTextInRect:CGRectMake(25, labelYPosition, 30, productNameLabel.frame.size.height)];

    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 25, labelYPosition);
    [productNameLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -25, -labelYPosition);

    labelYPosition += productNameLabel.frame.origin.y + productNameLabel.frame.size.height+20;
}
+4
source share
4 answers

Objective-c

CGSize sizeToFit = [title sizeWithFont:productNameLabel.font constrainedToSize:productNameLabel.frame.size lineBreakMode:productNameLabel.lineBreakMode];

Swift 2.2

var sizeToFit = title.sizeWithFont(productNameLabel.font, constrainedToSize: productNameLabel.frame.size, lineBreakMode: productNameLabel.lineBreakMode)

Swift3.0

var sizeToFit: CGSize = title.size(with: productNameLabel.font, constrainedTo: productNameLabel.frame.size, lineBreakMode: productNameLabel.lineBreakMode)
+2
source
CGSize constrainedSize = CGSizeMake(self.resizableLable.frame.size.width  , 9999);

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName,
                                      nil];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary];

CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

if (requiredHeight.size.width > self.resizableLable.frame.size.width) {
    requiredHeight = CGRectMake(0,0, self.resizableLable.frame.size.width, requiredHeight.size.height);
}
CGRect newFrame = self.resizableLable.frame;
newFrame.size.height = requiredHeight.size.height;
self.resizableLable.frame = newFrame;
0
source

, , , .

  func widthOfString(usingFont font: UIFont) -> CGFloat {

    let fontAttributes = [NSFontAttributeName: font]
    let size = self.size(attributes: fontAttributes)
    return size.width
}

UILabel

0
source

Swift 3:

Returns the height of the filled label depending on the text.

func heightForView(text: String, font: UIFont, width: CGFloat) -> CGFloat {

    let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = font
    label.text = text
    label.sizeToFit()
    return label.frame.height + labelHeightPadding ///extra padding; if needed be.
}
0
source

Source: https://habr.com/ru/post/1670985/


All Articles