是否可以在单个TextView中使用多种字体和大小?

时间:2018-04-20 02:45:44

标签: ios swift uitableview uitextview

我试图向用户呈现一个TableView,在表视图的每个单元格中都有一个图像和一个textView,我为textView.text提供了每个单元格的单个字符串,但是在每个字符串中,我我希望可能有多种字体。例如,字符串的一部分将具有20和粗体的大小,其余部分将具有14和Regular的大小。您可以在下图中看到我的应用程序中的内容

enter image description here

正如你所看到的,我试图编辑“你如何交易股票?”带有NSMutableAtributtedString的字符串的一部分,但输出是它的设置,而不是具有所选属性的字符串。我使用的代码如下:

let title1 = "How do you trade stocks?"

let atributtedTitle1 = NSMutableAttributedString(string: title1)

atributtedTitle1.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont(name: "AvenirNext-Bold", size: 20)!], range: getRangeOfSubString(subString: title1, fromString: title1))

var textViews:[String] = ["\(atributtedTitle1)The stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth. The stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealthThe stock market is one of the best ways you can build up wealth"]

加载textView文本通常是在带有

的tableView中完成的
cell.descriptionTextView.text = textViews[indexPath.row]

我用来计算NSMutableAtributtedString范围的函数如下:

func getRangeOfSubString(subString: String, fromString: String) -> NSRange {
    let sampleLinkRange = fromString.range(of: subString)!
    let startPos = fromString.distance(from: fromString.startIndex, to: sampleLinkRange.lowerBound)
    let endPos = fromString.distance(from: fromString.startIndex, to: sampleLinkRange.upperBound)
    let linkRange = NSMakeRange(startPos, endPos - startPos)
    return linkRange
}

如何在单个字符串中包含多个字体和大小,以便将其提供给单个textView?

1 个答案:

答案 0 :(得分:1)

作为您问题的直接答案:

  

是否可以在单个字符串中包含多个字体和大小   一个TextView?

是的。设置一个NSMutableAttributedString,它会得到所需的结果。例如,调用以下方法:

func setupTextViewAttributedString() {
    let myString = "The quick brown fox jumps over the lazy dog"
    let myAttributedString = NSMutableAttributedString(string: myString)
    myAttributedString.addAttribute(
        .font, value: UIFont.boldSystemFont(ofSize: 20),
        range: NSRange(location:0,length:4))

    myAttributedString.addAttributes([.foregroundColor : UIColor.red,
                                      .font: UIFont.boldSystemFont(ofSize: 20)],
                                     range: NSRange(location:0,length:19))

    myAttributedString.addAttributes([.foregroundColor : UIColor.blue,
                                      .font: UIFont.systemFont(ofSize: 14)],
                                     range: NSRange(location:20,length:4))

    myAttributedString.addAttributes([.foregroundColor : UIColor.green,
                                      .font: UIFont.systemFont(ofSize: 16)],
                                     range: NSRange(location:25,length:myString.count - 25))

    textView.attributedText = myAttributedString
}

会给出以下输出:

enter image description here

通过设置属性 - 使用给定范围 - (使用addAttributes(_:range:)),您甚至可以一次设置多个属性,如上所示,我设置了所需文本范围的字体和颜色。

此外:

如果您的目标是评估HTML,您可以这样做:

func evaluateHTMLString() {
    let htmlString = """
                        <font color=\"red\">The quick brown fox </font>
                        <font color=\"blue\">jumps </font>
                        <font color=\"green\">over the lazy dog</font>
                     """
    let attributedString = try! NSAttributedString(data: htmlString.data(using: .utf8)!,
                                                      options: [.documentType: NSAttributedString.DocumentType.html],
                                                      documentAttributes: nil)

    textView.attributedText = attributedString
}

输出:

enter image description here