【问题标题】:NSTextAlignment.Justified for UILabel does not workUILabel 的 NSTextAlignment.Justified 不起作用
【发布时间】:2015-01-19 12:30:32
【问题描述】:

我试图证明我的 UILabel 文本是正确的,但它不起作用。

声明我的UIView

descriptionUIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

声明我的UILabel

bottleDescriptionLabel = UILabel(frame: CGRect(x: widthMargin, y: bottleDescriptionTitleLabel.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin)))
bottleDescriptionLabel.font = UIFont(name: "AvenirNext-Regular", size: 16)
bottleDescriptionLabel.text = bottleDescriptionString
bottleDescriptionLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
bottleDescriptionLabel.textAlignment = NSTextAlignment.Justified
bottleDescriptionLabel.numberOfLines = 0

它看起来像这样:

我不知道还有什么可以使用NSTextAlignment.Justified 来证明我的文字的合理性。我应该改用UITextView 吗?

【问题讨论】:

    标签: swift uilabel text-alignment justify


    【解决方案1】:

    我一直在寻找答案,我只使用这一行而不是段落样式或 NSAttributedString。

    self.labelDescription.textAlignment = .justified
    

    【讨论】:

    • 是的,谢谢,你是对的。正如@kambala 所指出的,它在 iOS 10 中得到了更新。当问题发布时,iOS 10 还没有发布。
    【解决方案2】:
    func justifyLabel(str: String) -> NSAttributedString
    {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = NSTextAlignment.Justified
        let attributedString = NSAttributedString(string: str,
                                                  attributes: [
                                                    NSParagraphStyleAttributeName: paragraphStyle,
                                                    NSBaselineOffsetAttributeName: NSNumber(float: 0)
            ])
    
        return attributedString
    }
    

    像这样调用 justifyLabel() 函数...

    myLabel.attributedText = justifyLabel(myLabel.text!)
    

    【讨论】:

    • 在我的例子中,“NSBaselineOffsetAttributeName: NSNumber(float: 0)”是让它工作的关键。谢谢!
    【解决方案3】:

    Objective-C,完美的更新方案是在xCode 7和iOS 9上使用NSMutableParagraphStyle测试

    NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
            paragraphStyles.alignment = NSTextAlignmentJustified;      //justified text
            paragraphStyles.firstLineHeadIndent = 1.0;
            NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyles};
            NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: YourString attributes: attributes];
            YourLabel.attributedText = attributedString;
    

    【讨论】:

      【解决方案4】:

      您必须创建一个 NSMutableParagraphStyle 与一个 NSAttributedString 组合以显示文本是合理的。 重要的部分是将 NSBaselineOffsetAttributedName 设置为 0.0。

      这是一个如何将所有内容放在一起的示例:

      let sampleText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
      
      let paragraphStyle = NSMutableParagraphStyle()
      paragraphStyle.alignment = NSTextAlignment.Justified
      
      let attributedString = NSAttributedString(string: sampleText,
          attributes: [
              NSParagraphStyleAttributeName: paragraphStyle,
              NSBaselineOffsetAttributeName: NSNumber(float: 0)
          ])
      
      let label = UILabel()
      label.attributedText = attributedString
      label.numberOfLines = 0
      label.frame = CGRectMake(0, 0, 400, 400)
      
      
      let view = UIView()
      view.frame = CGRectMake(0, 0, 400, 400)
      view.addSubview(label)
      

      NSBaselineOffsetAttributedName 的学分:https://stackoverflow.com/a/19445666/2494219

      【讨论】:

      • 谢谢!我更喜欢保留 UILabel,这样我就不必处理“文本选择”、“滚动”等。所以你的解决方案很棒,谢谢。
      • 仅供参考:从 iOS 10 开始,这不再需要,似乎 UILabel 已修复使用 NSTextAlignment.Justified
      【解决方案5】:

      您可以使用 UITextView 来解决您的问题。

      bottleDescriptionTextView = UITextView(frame: CGRect(x: widthMargin, y: bottleDescriptionTextView.frame.maxY + heightMargin, width: self.view.frame.width - (2 * widthMargin), height: heightBottleDescription - (2 * heightMargin)))
      bottleDescriptionTextView.font = UIFont(name: "AvenirNext-Regular", size: 16)
      bottleDescriptionTextView.text = bottleDescriptionString
      bottleDescriptionTextView.textAlignment = NSTextAlignment.Justified
      

      【讨论】:

      • 谢谢,我试过了,但我更喜欢使用@Devran 解决方案,所以我可以保留一个 UILabel,这正是我想要的这个实现(至少现在,但我保留记住你的答案!)
      • 您不应该使用 UITextView 代替带有对齐文本的 UILabel
      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多