【问题标题】:How to have selected words in UITableView footer in bold typeface in Swift?如何在 Swift 中以粗体字体在 UITableView 页脚中选择单词?
【发布时间】:2019-12-30 01:11:09
【问题描述】:

我正在创建一个字符串数组以与titleForFooterInSection 表视图委托方法一起使用。 每个字符串将跨越几行,并且需要强调一些单词。

我怎样才能让字符串中的选定单词只有粗体字?

我想实现这张图片中的内容:

谢谢

【问题讨论】:

  • NSAttributedString 完全可以存储在数组中。
  • 我如何仅将给定字符串的某些选定单词更改为粗体字?我发现了这个问题:stackoverflow.com/questions/12974120/…rangeOfString 不适用于 NSMutableAttributedStrings。
  • 为什么这被否决了?它有什么问题?

标签: swift uitableview footer nsattributedstring nsattributedstringkey


【解决方案1】:

我在某个项目中所做的是我创建了一个像这样的对象:

struct StringWithStyle {
    let font: UIFont
    let color: UIColor
    let text: String
    let backgroundcolor: UIColor

    init(font: UIFont,
         color: UIColor,
         text: String,
         backgroundColor: UIColor = .clear) {
        self.font = font
        self.color = color
        self.text = text
        self.backgroundcolor = backgroundColor
    }

    var mutableAttrString: NSMutableAttributedString {
        let attributes = [NSAttributedString.Key.font: font,
                          NSAttributedString.Key.foregroundColor: color,
                          NSAttributedString.Key.backgroundColor: backgroundcolor]
        return NSMutableAttributedString(string: text, attributes: attributes)
    }
}

您当然可以将字体设置为保持不变或创建应用中使用的通用样式。

然后我有一个扩展来传递带有样式的文本

static func textWithMultipleStyles(_ styles: [StringWithStyle]) -> NSMutableAttributedString {
    var allTextStyles = styles
    let text = allTextStyles.removeFirst().mutableAttrString
    guard !allTextStyles.isEmpty else {
        return text
    }
    for nextText in allTextStyles {
        text.append(nextText.mutableAttrString)
    }
    return text
}

使用你:

let example = String.textWithMultipleStyles([StringWithStyle(font: UIFont.boldSystemFont(ofSize: 16.0),
                                                      color: .black,
                                                      text: "First String"),
                                          StringWithStyle(font: UIFont.systemFont(ofSize: 13, weight: .semibold),
                                                      color: .red,
                                                      text: "Second string")])

也许有更好的方法,但对我来说,我在应用程序中使用了 3-4 种常用样式,并且可以轻松构建多个样式字符串。

否则你可以使用范围

let boldText = "Some bold text"
let message = "This is a sentence with bold text \(boldText)"
let range = (message as NSString).rangeOfString(boldText)
let attributedString = NSMutableAttributedString(string: message)
attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(label.font.pointSize), range: range)
label.attributedText = attributedString

【讨论】:

  • StringStyle 类型从何而来?然后如何将此方法应用于字符串选定部分的一部分?
  • 是的,抱歉。 StringWithStyle 应该无处不在。我更改并添加了一个示例
  • 谢谢!这很有趣,但我不确定我是否能够使用它,因为我想让应用程序保持可访问性,即使用 .preferredFontStyle(for:)... 此文本将位于分组表视图的页脚和每个页脚将有几行,其中一些单词必须是粗体。我不知道这是否更清楚。
  • 然后你必须使用特定单词的范围,我不太喜欢在代码中使用这些单词。这种方式可能会使字符串的构造占用更多的行,但它更直接,至少对我而言,并且以后更容易调试和更改。
  • 我认为我的答案适用于它,我不明白preferredFontStyle 的问题。范围答案也应该有效。
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
相关资源
最近更新 更多