【问题标题】:How to enable automatic hyphenation of words by syllables?如何启用按音节自动断字?
【发布时间】:2020-10-07 09:07:08
【问题描述】:

我有 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")
        }

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

我有:

2010 年 6 月在
全球
开发者大会
苹果
公布版本
Xcode 4
开发者期间
工具状态
工会地址。

我需要:

2010 年 6 月在
全球
开发者大会-
参考,苹果
宣布版本-
Xcode 的第 4 版
在开发期间-
操作工具状态
联盟的广告-
着装。

【问题讨论】:

    标签: html css ios swift wkwebview


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-06-28
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2018-06-10
      • 2016-09-11
      • 2013-05-09
      • 1970-01-01
      • 2022-10-31
      相关资源
      最近更新 更多