Swift 3 NSMutableParagraphStyle:NSLineBreakMode.byWordWrapping继续打破单词

时间:2017-08-16 00:31:48

标签: ios swift3 fonts nsmutablestring

我有一个NSMutableParagraphStyle样式,我应用于NSMutableAttributedString:

import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let myString = "Before a telephone call, I need to go to the bathroom..."
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
        let myMutableString = NSMutableAttributedString(
            string: myString,
            attributes: [
                NSFontAttributeName:UIFont(
                    name: "Georgia",
                    size: 45.0
                    )!,
                NSParagraphStyleAttributeName: paragraphStyle
            ]
        )
        let textView = UITextView( frame: CGRect(
            x: 20,
            y: 25,
            width: 260,
            height: 400
        ) )
        textView.attributedText = myMutableString
        self.view.addSubview( textView )
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

但是当我将可变字符串添加到UITextView时,它会不断破坏单词。

当我去paragraphStyle.lineBreakMode = NSLineBreakMode.byCharWrapping时,单词中断不会出现在同一个地方......所以它确实有效果。但这仍然是局部的。我有«call»这个词在«c»之后打破了,这太超级怪了。

wtf

这是我可以在故事板编辑器中重现的内容:

enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

这是预期的行为。根据Apple的文档,

byWordWrapping

  

包装发生在单词边界,除非单词   本身不适合单行。见字符和字素   字符串编程指南中的集群,用于讨论问题   与确定单词边界有关。

<强> byCharWrapping

  

包装发生在第一个不适合的角色之前。

有关详细信息,请参阅Apple's Documentation

答案 1 :(得分:0)

Aaaaaannnnddd解决方案是尝试在崇高文本中编辑我的代码,并意识到电话和电话之间存在不可破坏的空间。

这似乎是一个非常愚蠢的基本功能。为什么Xcode不能告诉我......