根据字符串长度绘制UILabel

时间:2017-02-28 03:32:34

标签: ios objective-c swift

我使用drawRect绘制标签,代码如下所示。

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

然而,productNameLabel.numberOfLiness = 2似乎根本不起作用...如果字符串的长度超过屏幕宽度,则文本被截断,UILabel保持一个衬垫。

任何人都知道我该怎么做,所以如果字符串的长度超过屏幕的宽度,超出的单词将转到第二行?

谢谢!

更新了代码,仍然无法运作!

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 个答案:

答案 0 :(得分:2)

<强>目标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)

答案 1 :(得分: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;

答案 2 :(得分:0)

实现此方法以查找字符串传递字体的宽度作为要为字符串提供的参数,此方法将返回字符串的宽度。

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

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

将此宽度设置为UILabel的宽度

答案 3 :(得分:0)

<强>夫特-3:

  

根据文本返回带有填充的标签高度。

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