UILabel自动换行

问题描述 投票:1回答:2

我有一个UILabel设置为最多有2行,当内容太多时会自动换行。

我有一个问题,当只剩下一个单词时,它不会打扰并截断文本。

无论剩下多长时间,我怎么能把它包装好所有文本呢?

(下面的第一个例子只是缺少'aves'

ios objective-c uilabel word-wrap
2个回答
1
投票

你缺少一些信息,所以这基本上是一个非常有根据的猜测:

我假设您正在使用Autolayout(可能应该标记它)。

如果您将标签的preferredMaxLayoutWidth设置为高于实际使用的值,则可能会导致您看到的行为。

我玩了一下,现在可以重现结果。

Playground Screenshot

如您所见(单击它),我在第13行将preferredMaxLayoutWidth设置为310.但标签的实际宽度为299点(第36行)。

Autolayout引擎将使用310而不是299来计算需要多少行。第一个字符串适合310 pt的行。宽,第二根弦没有。这就是为什么只截断第一个标签中的文本。

解决方案是不设置preferredMaxLayoutWidth。但这可能会对性能产生影响或产生任何其他负面影响。在这种情况下,您必须找到一种方法将其设置为正确的值。

这是完整的游乐场,万一有人想玩它:

// Playground - noun: a place where people can play

import UIKit

var view = UIView(frame: CGRect(x: 0, y: 0, width: 315, height: 200))
view.backgroundColor = UIColor.lightGrayColor()

func addLabel(view: UIView) -> UILabel {
    let label1 = UILabel()
    label1.numberOfLines = 0
    label1.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.1)
    label1.setTranslatesAutoresizingMaskIntoConstraints(false)
//    label1.preferredMaxLayoutWidth = 310
    view.addSubview(label1)
    return label1
}

let label1 = addLabel(view)
let label2 = addLabel(view)

let views = ["l1" : label1, "l2" : label2]

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[l1]-8-[l2]", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[l1]-|", options: nil, metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[l2]-|", options: nil, metrics: nil, views: views))

label1.text = "How to Wear This Spring's Must-Haves"
label2.text = "The Best New Men's Fragrances For Spring 2015"


view.layoutIfNeeded()

view.frame
label1.frame
label1.preferredMaxLayoutWidth

label2.frame
label2.preferredMaxLayoutWidth

0
投票

1.使用以下代码片段计算标签的高度

-(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{
    CGSize constraint = CGSizeMake(width , 20000.0f);
    CGSize title_size;
    float totalHeight;

    SEL selector = @selector(boundingRectWithSize:options:attributes:context:);
    if ([text respondsToSelector:selector]) {                
        title_size = [text boundingRectWithSize:constraint
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{ NSFontAttributeName : font }
                                        context:nil].size;

        totalHeight = ceil(title_size.height); 
    } else {                
        title_size = [text sizeWithFont:font
                      constrainedToSize:constraint
                          lineBreakMode:NSLineBreakByWordWrapping];                
        totalHeight = title_size.height ;                
    }

    CGFloat height = MAX(totalHeight, 40.0f);
    return height;            
}

2.创建这样的标签

UILabel *YourLabel = [[UILabel alloc] init];

YourLabel.numberOfLines = 0;

YourLabel.lineBreakMode =  NSLineBreakByWordWrapping;

CGRect frame = YourLabel.frame;

float height = [self getHeightForText:YourLabel.text 
                             withFont:YourLabel.font
                            andWidth:YourLabel.frame.size.width];
float gap = 2;

YourLabel.frame = CGRectMake(frame.origin.x, 
                                         frame.origin.y, 
                                         frame.size.width, 
                                         height);

希望这可以帮助

Attribution

© www.soinside.com 2019 - 2024. All rights reserved.