4

I am drawing label using drawRect and the code looks like something 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, the productNameLabel.numberOfLiness = 2 doesn't seem to work at all... If the string has length that exceeds the width of the screen, the text is truncated and the UILabel stays one liner.

Anyone knows how do i do it, so that if the length of the string exceeds the width of screen, the exceeded words will go to the second line?

Thanks!

updated code, still doesn't work !

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;
}
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
progammingBeignner
  • 936
  • 1
  • 8
  • 19

4 Answers4

3

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)
sumit kumar
  • 313
  • 1
  • 12
0
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;
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
0

Implement this method to find width of string pass font as an argument you want to give to string and this method will return width of string.

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

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

Set this width as a width of the UILabel

0

Swift-3:

Returns the height of label with padding 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.
}
Zarif Ahmed
  • 343
  • 2
  • 9