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

时间:2015-05-18 10:05:50

标签: ios uikit justify

见下文:

// 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()

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

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

请参阅: enter image description here

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

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

1 个答案:

答案 0 :(得分:7)

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

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