【问题标题】:Drawing UILabel according to the string length根据字符串长度绘制UILabel
【发布时间】:2017-07-18 21:20:42
【问题描述】:

我正在使用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;
}

【问题讨论】:

  • 是否使用自动布局选项?
  • 朋友,您使用的是 AutoLayout 还是 Autoresizing
  • 用简单标签注释掉drawRect 代码的检查,为标签设置框架。这是为了调试问题所在。

标签: ios objective-c swift


【解决方案1】:

Objective-c

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

斯威夫特 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】:
    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;
    

    【讨论】:

    • 帧计算正确。。但是,多余的文本被截断,没有到第二行。。UILabel 仍然只显示一个行。
    • 你在使用自动布局吗
    • 不,它不起作用...对于这一部分,我正在使用核心图形以编程方式编写 UILabel,而不使用自动布局。
    • 创建标签时添加此代码productNameLabel.numberOfLines = 0;
    【解决方案3】:

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

      func widthOfString(usingFont font: UIFont) -> CGFloat {
    
        let fontAttributes = [NSFontAttributeName: font]
        let size = self.size(attributes: fontAttributes)
        return size.width
    }
    

    将此宽度设置为UILabel的宽度

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-19
        • 2016-07-08
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 2015-09-05
        • 2023-04-04
        相关资源
        最近更新 更多