Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3:文本对齐失败_Ios_Swift_Xcode_Uilabel - Fatal编程技术网

Ios Swift 3:文本对齐失败

Ios Swift 3:文本对齐失败,ios,swift,xcode,uilabel,Ios,Swift,Xcode,Uilabel,我正在尝试将我的UILabels textAlignment设置为。right,但它根本不起作用。好像它根本就不在那里。这是我的密码: let productDescriptionLabel = UILabel(frame: CGRect(x: 10, y: imageViewHeight + productTitleLabel.frame.size.height + 10, width: viewWidth - 20, height: 0)) productDescriptionLabel.t

我正在尝试将我的
UILabel
s textAlignment设置为
。right
,但它根本不起作用。好像它根本就不在那里。这是我的密码:

let productDescriptionLabel = UILabel(frame: CGRect(x: 10, y: imageViewHeight + productTitleLabel.frame.size.height + 10, width: viewWidth - 20, height: 0))
productDescriptionLabel.textColor = UIColor.darkGray
productDescriptionLabel.textAlignment = .right
productDescriptionLabel.text = productsDescriptionArray!
productDescriptionLabel.font = productDescriptionLabel.font.withSize(self.view.frame.height * self.relativeFontConstant)
productDescriptionLabel.numberOfLines = 0
let attrString = NSMutableAttributedString(string: productDescriptionLabel.text!)
let style = NSMutableParagraphStyle()
style.lineSpacing = 10
style.minimumLineHeight = 20
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: (productDescriptionLabel.text?.characters.count)!))
productDescriptionLabel.attributedText = attrString
productDescriptionLabel.sizeToFit()
productDescriptionLabel.lineBreakMode = .byWordWrapping
contentScrollView.addSubview(productDescriptionLabel)

它显示的所有内容都是

,因为您的目标是通过
NSMutableParagraphStyle
实例(
style
)编辑标签属性字符串,所以您应该通过编辑its属性将所需对齐方式指定给
样式

style.alignment = .right
另外,由于
style
处理对齐,因此无需实现:

productDescriptionLabel.textAlignment = .right

删除
productDescriptionLabel.textAlignment=.right
而不是
style.alignment=.right
?你可以用故事板来做,检查一下may answer@inspector\u 60这对OP案例没有用,他使用的是
NSMutableParagraphStyle
,因此不适用于标签。请注意,他已经通过实现
productDescriptionLabel.textAlignment=.right
以编程方式实现了您在回答中提到的内容。我建议查看我张贴的答案:)