swift:用JSON编辑文本

时间:2018-03-31 18:28:46

标签: ios swift

我有带文本和标签的JSON文件。在JSON文件中,我想将这些字符<b><i>添加到某些单词中。例如:

"this is my text <b>bold<b> or <i>italic<i>"

之前,当我在标签中显示文字时,我想分析我的文字,如果某些单词包含此字符<b><i>,我想在标签文字中将此字词设为粗体或斜体。怎么做?或者还有另一种方法可以从JSON制作粗体文本?

更新:

您的代码:

/* Set Tagged Text & Fonts */
let taggedTextString = myText.text
let tagFont = UIFont.boldSystemFont(ofSize: 17.0)
let normalFont = UIFont.systemFont(ofSize: 17.0)

/* You can simply assign the AttributedText to the modified String, which may return nil. */
textLabel.attributedText = taggedTextString.modifyFontWithTags(openingTag: "<tag>", closingTag: "</tag>", taggedFont: tagFont, unTaggedFont: normalFont)

我的代码:

var attributedString = NSMutableAttributedString(string: myText.text)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
textLabel.attributedText = attributedString;

此代码是您和我的代码结合的最佳方式吗?

for i in stride(from: 0, to: allTagsSplitArray.count, by: 1) {
finalAttrStr.append(NSMutableAttributedString(string: allTagsSplitArray[i], attributes: [kCTFontAttributeName as NSAttributedStringKey : i % 2 == 0 ? untaggedTextFont : taggedTextFont]))

// my code

finalAttrStr.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, finalAttrStr.length))
        }

1 个答案:

答案 0 :(得分:0)

我似乎完成此操作的最简单方法是遍历字符串并扫描您的关键标记 - <b><i>等。

为了使事情变得更容易(并且更具可重用性),我们应该制作extensionString个类NSAttributedString。对于此示例,我已扩展String类,并返回NSAttributedString

func modifyFontWithTags(openingTag: String, closingTag: String, taggedFont: UIFont?, unTaggedFont: UIFont?) -> NSMutableAttributedString? {
    /* Make sure we have everything we need. */
    guard let taggedTextFont = taggedFont,
        let untaggedTextFont = unTaggedFont,
        !self.isEmpty,
        self.contains(openingTag),
        self.contains(closingTag) else { return nil }
    /* Split the string up using our closing tag. */
    let closingTagSplitArray = self.components(separatedBy: closingTag)
    /* Make a placeholder array. */
    var allTagsSplitArray = [String]()
    /* Iterate through our split array. */
    for item in closingTagSplitArray {
        if item.contains(openingTag) {
            /* Strip the opening tag & append. */
            allTagsSplitArray.append(contentsOf: item.components(separatedBy: openingTag))
        } else {
            /* Just append. */
            allTagsSplitArray.append(item)
        }
    }
    /* Instantiate our attributed string. */
    let finalAttrStr = NSMutableAttributedString()
    for i in stride(from: 0, to: allTagsSplitArray.count, by: 1) {
        /* Add our font to every-other item in the array (the tagged portions). */
        finalAttrStr.append(NSMutableAttributedString(string: allTagsSplitArray[i], attributes: [NSFontAttributeName : i % 2 == 0 ? untaggedTextFont : taggedTextFont]))
    }

    return finalAttrStr
}

注意:在此示例中,我接受UIFont参数,但您可以修改此函数以接受您想要的任何属性类型。