UILabel的NSTextAlignment.Justified不起作用

时间:2014-11-21 11:14:23

标签: swift uilabel text-alignment justify

我正在尝试证明我的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

它看起来像这样:

enter image description here

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

5 个答案:

答案 0 :(得分:29)

您必须与NSAttributedString一起创建NSMutableParagraphStyle,以便将文本显示为对齐。 重要的是将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

的积分

答案 1 :(得分:4)

myLabel.attributedText = justifyLabel(myLabel.text!)

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    static int i=0
    if(i==0)
        return 1;
        i++;
    else{
        return [self.flightDetailsArray count];
    }


}

答案 2 :(得分:2)

我一直在面对这个问题并寻找答案,我只使用这一行而不是paragraphStyle 或NSAttributedString。

self.labelDescription.textAlignment = .justified

答案 3 :(得分:-1)

您可以使用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

答案 4 :(得分:-1)

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;