【问题标题】:Is it possible to have multiple fonts and sizes in a single TextView?单个 TextView 中是否可以有多种字体和大小?
【发布时间】:2018-04-20 02:45:43
【问题描述】:

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

如您所见,我尝试编辑“您如何交易股票?” 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?

【问题讨论】:

标签: ios swift uitableview uitextview


【解决方案1】:

直接回答您的问题:

在一个字符串中是否可以有多种字体和大小 一个文本视图?

是的,有。按照应有的方式设置 aNSMutableAttributedString 将获得所需的结果。比如调用如下方法:

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
}

会给出以下输出:

通过在给定范围内设置属性(使用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
}

输出:

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 2012-08-17
    • 1970-01-01
    • 2016-03-30
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多