UILabel byWordWrapping 行为不当

UILabel byWordWrapping misbehaving

这是我的 UILabel 代码...

    var maximumLabelSize: CGSize = (chosenSpecies.identifyingCharacteristics![index] as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(16)])
    var identifyingCharacteristicsInformation = UILabel(frame: (CGRectMake(leftMargin, spacingValue, maximumLabelSize.width, maximumLabelSize.height))) 
    identifyingCharacteristicsInformation.font = UIFont(name: "HelveticaNeue-Light", size: informationTextSize)
    identifyingCharacteristicsInformation.textAlignment = NSTextAlignment.Left
    identifyingCharacteristicsInformation.text = chosenSpecies.identifyingCharacteristics![index]
    identifyingCharacteristicsInformation.text = identifyingCharacteristicsInformation.text!.capitalizedString
    identifyingCharacteristicsInformation.textColor=UIColor(red:0.0, green:0.0, blue:0.0, alpha:1)
    identifyingCharacteristicsInformation.numberOfLines = 0
    identifyingCharacteristicsInformation.lineBreakMode = NSLineBreakMode.ByWordWrapping
    identifyingCharacteristicsInformation.preferredMaxLayoutWidth = pictureContainer.frame.width                  
    ScrollView.addSubview(identifyingCharacteristicsInformation)
    spacingValue += identifyingCharacteristicsInformation.frame.height
    scrollView.contentSize.height += identifyingCharacteristicsInformation.frame.height

事情是这样的。这些 'Characteristics' 中的每一个都是使用上面的代码在 for 循环中打印的。如您所见,长 运行 的特征就在屏幕之外。

http://imgur.com/hbinE5n

我一直在网上搜索为什么这可能会发生几个小时并且一直没有结果。有没有人知道为什么会发生这种情况?由于我的理解并不完整,因此我在使用自动换行时遇到了困难。只需实现这两行,我就可以在我的代码中的其他地方应用它。

label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping

以下是我对Eric Qian建议的尝试实现。我目前正在努力应用它。

var maxSize: CGSize = CGSize(width: 320, height: 100)
var maximumLabelSize: CGRect = (chosenSpecies.identifyingCharacteristics![index] as NSString).boundingRectWithSize(maxSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16)], context: nil)

检查标签的宽度。我相信它超出了屏幕范围。您需要使用

CGSize maxSize = CGSizeMake(320.0, 1000.0);
CGSize maximumLabelSize = [string boundingRectWithSize:maxSize
                     options:NSStringDrawingUsesLineFragmentOrigin
                  attributes:@{NSFontAttributeName: UIFont.systemFontOfSize(16)} 
                     context:nil];

而不是 sizeWithAttributes 以获得字符串的正确大小。由于 sizeWithAttributes 将整个字符串呈现在一行中。