在iOS(Swift或Objective-C)中对齐文本而不会破坏跟踪/字距调整/字母间距

eri*_*osg 6 justify uikit ios

见下文:

// Description (HTML string):
var attrStr = NSMutableAttributedString(
    data: formatted.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
    documentAttributes: nil,
    error: nil)

var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
attrStr?.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attrStr!.length))
attrStr?.addAttribute(NSKernAttributeName, value: -0.1, range: NSMakeRange(0, attrStr!.length))
descLabel.attributedText = attrStr
descLabel.sizeToFit()
Run Code Online (Sandbox Code Playgroud)

到目前为止,我试图证明文本的合理性,导致了同样的行为.我已经通过内联式CSS int eh html文本或使用textAlignment属性尝试过.

问题是,证明文本的合理性会打破字母间距(实际上它会增加每个单词的跟踪/字距,而不是仅增加其间的空间).

看到: 在此输入图像描述

我想看到一个不是黑客的解决方案,因为我已经设计了一些我自己的黑客,但后来我陷入了其他问题的太多可能性.

我错过了一些简单的东西?我已经浏览了所有UI选项,并且在我的代码示例中看到,我甚至尝试更改Kern,它确实发生了变化但只是按比例变化(即它仍然增加).

Enr*_*tyo 7

您是否尝试过添加1的NSParagraphStyle属性hyphenationFactor

这将保持你的字距和单词间距,但是当单词需要突破到下一行时会添加连字符.

  • 在我的解决方案中实现,但如果有人仍想回答,我想知道如何在证明时保持自动换行+字距调整/跟踪. (2认同)