不能删除具有字符串插值的文本?
Can't strikethrough text that has string interpolation?
我正在尝试在我的子任务文本之前添加一个项目符号点,然后添加一个删除线。问题是,如果我使用字符串插值并在我的子任务名称之前加上任何内容,则删除线不会显示。但是,如果我将按钮标题设置为我的子任务名称并尝试添加删除线,它会起作用。
subtaskButton.setTitle("- \(subtask.name)", for: .normal)
strikethrough(text: subtask.name, label: subtaskButton.titleLabel!)
private func strikethrough(text: String, label: UILabel) {
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributedString.length))
label.attributedText = attributedString
}
^ 无效
subtaskButton.setTitle("\(subtask.name)", for: .normal)
subtaskButton.setTitle(subtask.name, for: .normal)
^ 这两个都有效
尝试使用 setAttributedTitle(_:for:)
而不是直接设置标签的 attributedText
。
private func strikethrough(text: String, button: UIButton) {
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributedString.length))
button.setAttributedTitle(attributedString, for: .normal)
}
我正在尝试在我的子任务文本之前添加一个项目符号点,然后添加一个删除线。问题是,如果我使用字符串插值并在我的子任务名称之前加上任何内容,则删除线不会显示。但是,如果我将按钮标题设置为我的子任务名称并尝试添加删除线,它会起作用。
subtaskButton.setTitle("- \(subtask.name)", for: .normal)
strikethrough(text: subtask.name, label: subtaskButton.titleLabel!)
private func strikethrough(text: String, label: UILabel) {
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributedString.length))
label.attributedText = attributedString
}
^ 无效
subtaskButton.setTitle("\(subtask.name)", for: .normal)
subtaskButton.setTitle(subtask.name, for: .normal)
^ 这两个都有效
尝试使用 setAttributedTitle(_:for:)
而不是直接设置标签的 attributedText
。
private func strikethrough(text: String, button: UIButton) {
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributedString.length))
button.setAttributedTitle(attributedString, for: .normal)
}