UITextField不显示NSFontAttributes

时间:2015-05-10 19:54:57

标签: html ios swift nsattributedstring

我在Swift写作,有一个应用程序,其中包含多个用户可编辑的textFields和textViews,所有这些都具有attributesText编辑功能。

编辑完成后,信息将保存到CoreData对象,并上传到服务器进行备份。服务器将AttributedStrings保存为HTML,并在下载/同步时将其转换回NSAttributedString。

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

任何想法为什么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(WORKS):

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

textField(DOESN&#39; T WORK):

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

1 个答案:

答案 0 :(得分:0)

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

相关问题