Remark:
实施:
myLabel.textAlignment = .right
does not解决了我的问题,这不是我所要求的。
我想要实现的是让标签对齐右对齐.
为了更清楚地说明:
这就是如何left
对齐外观:
就是这样justify
对齐外观:
if you are not seeing any difference between the tow screenshots, you could recognize it by the right (trailing) constraint of the label, you will notice in the second screenshot the whole width of the label has been filled by the text.
如第二张截图所示,让标签对齐即可左对齐默认情况下,即最后一行对齐方式是left
.
我怎样才能让标签向右对齐?换句话说,我希望文本与第二个屏幕截图一样,只是我希望最后一个短行向右移动。
If you wondering what is the purpose of doing such a thing, it would be appropriate for right to left languages.
谢谢@用户6788419 https://stackoverflow.com/users/6788419/user6788419提到适当的答案 https://stackoverflow.com/a/31283642/5501940.
为了实现这一目标,您应该与NSMutableParagraphStyle https://developer.apple.com/documentation/uikit/nsmutableparagraphstyle:
一个对象,可以更改子属性的值
段落样式属性。
它使您能够alignment
and baseWritingDirection
同时,这会产生所需的输出:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lblDescription: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let text: NSMutableAttributedString = NSMutableAttributedString(string: "In vertebrates, it is composed of blood cells suspended in blood plasma. Plasma, which constitutes 55% of blood fluid, is mostly water (92% by volume), and contains dissipated proteins...")
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.baseWritingDirection = .rightToLeft
paragraphStyle.lineBreakMode = .byWordWrapping
text.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.length))
lblDescription.attributedText = text
}
}
输出将是:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)