如何启用按音节自动断字?

How to enable automatic hyphenation of words by syllables?

我有 html 和 css 文件可以在我的应用程序中显示我的教科书。我有这个代码来做:

let headerString = "<meta name=\"viewport\" content=\"initial-scale=1.1\" />"
do {
            guard let filePath = Bundle.main.path(forResource: "\(readBookNumber)", ofType: "html")
                else {
                    print ("File reading error")
                    return
                }
            var content =  try String(contentsOfFile: filePath, encoding: .utf8)
            let baseUrl = URL(fileURLWithPath: filePath)
            
            content.changeHtmlStyle(font: "Iowan-Old-Style", fontSize:  UserDefaults.standard.integer(forKey: "textSize"), fontColor: textColor)
            webView.loadHTMLString(headerString+content, baseURL: baseUrl)
        }
        catch {
            print ("File HTML error")
        }

如何启用按音节自动断字?

我有:

In June 2010 at
the World Wide
Developers Conference
Apple
announced version
4 of Xcode
during the Developer
Tools State
of the Union address.

我需要:

In June 2010 at
the World Wide
Developers Con-
ference, Apple
announced ver-
sion 4 of Xcode
during the Devel-
oper Tools State
of the Union ad-
dress.

您可以为此使用段落样式:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
paragraphStyle.hyphenationFactor = 1.0
paragraphStyle.lineBreakMode = .byWordWrapping
let attributes = [
                  NSAttributedString.Key.paragraphStyle: paragraphStyle,
                  NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)
                 ]

let attributedText = NSAttributedString(string: inputText, attributes: attributes)