在iOS(Swift或Objective-C)中对齐文本而不会破坏跟踪/字距调整/字母间距 [英] Justify text in iOS (Swift or Objective-C) without breaking tracking/kerning/letter spacing

查看:314
本文介绍了在iOS(Swift或Objective-C)中对齐文本而不会破坏跟踪/字距调整/字母间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

见下文:

// 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 属性。

So far all my attempts to justify text, result in the very same behavior. I've tried via inline-style CSS int eh html text, or using textAlignment property.

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

The issue is, justifying the text breaks the letter spacing (actually it increases the tracking/kerning of each word instead of only increasing the space in-between).

请参阅:

See:

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

I would like so see a solution that is not a hack, since I have devised some hacks of my own but then I fall into too many possibilities for other issues.

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

I'm I missing something simple? I've been through all the UI options and as seen in my code sample, I even tried changing the Kern, which does change but only in proportion (i.e. it still increases).

推荐答案

您是否尝试使用 hyphenationFactor 添加 NSParagraphStyle 属性?

Have you tried adding a NSParagraphStyle attribute with hyphenationFactor of 1?

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

This will keep your kerning and word spacing, but will add hyphenation when the word needs to break to the next line.

这篇关于在iOS(Swift或Objective-C)中对齐文本而不会破坏跟踪/字距调整/字母间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆