UITextField 不显示 NSFontAttributes

UITextField not displaying NSFontAttributes

我在 Swift 写作,有一个包含多个用户可编辑的文本字段和文本视图的应用程序,它们都具有属性文本编辑功能。

编辑后,信息将保存到 CoreData 对象并上传到服务器进行备份。服务器将AttributedStrings保存为HTML,当downloaded/synced.

时又转换回NSAttributedString

所有这些都有效(虽然我得到了很多额外的属性,比如添加了段落样式),但出于某种原因,我的文本字段s,一旦保存并重新加载,不显示任何斜体或粗体字体属性(即使它存在,上传、下载和转换都很好)。然而 textViews,经过相同的过程,显示文本就好了。

知道为什么 textField 不起作用而 textView 可以显示属性文本吗?

这是一些代码,如果我遗漏了一些必要的东西,我可以添加更多:

示例 HTML 字段文本:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Optima; color: #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: 'Optima'; font-weight: bold; font-style: normal; font-size: 16.00pt; font-kerning: none; text-shadow: 0.0px -1.0px 0.0px rgba(255, 255, 255, 0)}
span.s2 {font-family: 'Optima-Regular'; font-weight: normal; font-style: normal; font-size: 16.00pt; font-kerning: none; text-shadow: 0.0px -1.0px 0.0px rgba(255, 255, 255, 0)}
span.s3 {font-family: 'Optima'; font-weight: normal; font-style: italic; font-size: 16.00pt; font-kerning: none; text-shadow: 0.0px -1.0px 0.0px rgba(255, 255, 255, 0)}
span.s4 {font-family: 'Optima-Regular'; font-weight: normal; font-style: normal; font-size: 16.00pt; text-decoration: underline ; font-kerning: none; text-shadow: 0.0px -1.0px 0.0px rgba(255, 255, 255, 0)}
</style>
</head>
<body>
<p class="p1"><span class="s1">Check</span><span class="s2"> </span><span class="s3">for</span><span class="s2"> </span><span class="s4">title</span><span class="s2"> here. <span class="Apple-converted-space">   </span></span></p>
</body>
</html>

NSAttributeString / HTML 转换器:

class AttrTextConverter {
func toHtmlString(attrString: NSAttributedString) -> String {
    if let toData = attrString.dataFromRange(NSMakeRange(0, attrString.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], error: nil) {
        let htmlNSString = NSString(data: toData, encoding: NSUTF8StringEncoding)
        let htmlString = htmlNSString as! String
        return htmlString
    }
    return ""
}

func toAttrString(htmlString: String) -> NSAttributedString {
    let htmlNsString = htmlString as NSString
    let htmlData = htmlNsString.dataUsingEncoding(NSUTF8StringEncoding)
    var concreteString : NSAttributedString = NSAttributedString(string: "")
    if let attrString = NSMutableAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil) {
        attrString.enumerateAttribute(NSFontAttributeName, inRange: NSMakeRange(0, attrString.length), options: NSAttributedStringEnumerationOptions.allZeros) { value, range, stop in
            if value != nil {
                let oldFont = value as! UIFont
                let oldName = oldFont.fontName
                var newFont = defaultFont
                if oldName.hasSuffix("-BoldOblique") || oldName.hasSuffix("-BoldItalic") {
                    newFont = defaultBoldItalic
                } else if oldName.hasSuffix("-Bold") {
                    newFont = defaultBold
                } else if oldName.hasSuffix("-Italic") || oldName.hasSuffix("-Oblique") {
                    newFont = defaultItalic
                }
                attrString.removeAttribute(NSFontAttributeName, range: range)
                attrString.addAttribute(NSFontAttributeName, value: newFont, range: range)
            } else {
                attrString.addAttribute(NSFontAttributeName, value: defaultFont, range: NSMakeRange(0, attrString.length))
            }
        }
        concreteString = attrString
    }

    return concreteString
}
}

这是viewController中的显示代码:

textView(作品):

if let mutStr = lesson?.valueForKey(record) as? NSAttributedString {
                let newStr = fontSet.changeFontFamily(mutStr)
                view.attributedText = newStr
            }

文本字段(不起作用):

if let mutStr = lesson?.valueForKey(record) as? NSAttributedString {
                let newStr = fontSet.changeFontFamily(mutStr)
                field.attributedText = newStr
            }

我的解决方法是用小文本视图替换文本字段。我想这没什么大不了的,但令人沮丧的是 textFields 在技术上应该能够处理属性字符串。