为什么NSAttributedString不能与UITableView中的textLabel一起使用,但是UILabel可以呢?

问题描述 投票:1回答:1

我在两种类型的UILabel中测试该代码。

让我们从UILabel开始:

让我们基于在UIViewController中上传的UILabel连接故事板。

 @IBOutlet weak var codinglabel: UILabel!

然后,让我们为UIFont创建一个SF Mono。但是,iOS 13要求此SF Mono。

let sfMonoDescriptor = UIFont.systemFont(ofSize: 15).fontDescriptor.withDesign(.monospaced)
let sfMonoFont = UIFont(descriptor: sfMonoDescriptor!, size: 15)
codinglabel.font = sfMonoFont

让我们为UILabel创建文本以在标签中显示字符串。

codinglabel.text = "var greeting = \"Hello World\""

现在,它开始创建NSAttributedString。

let attributedCoding = NSMutableAttributedString(string: codinglabel.text!)

let oneCodeconecpt = (codinglabel.text! as NSString).range(of: "var")

attributedCoding.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemPink , range: oneCodeconecpt)

let oneCodeconecptTwo = (codinglabel.text! as NSString).range(of: "\"Hello World\"")

attributedCoding.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.systemRed , range: oneCodeconecptTwo)

codinglabel.attributedText = attributedCoding

自执行应用以来,UILabel在UILabel中具有完美的作品!

屏幕截图:https://ibb.co/Yk5Ktkw

现在,我必须对UITableView的单元标签(称为textLabel)进行测试。结果如下:

if indexPath.row == 1 {

code = OperatorTableView.dequeueReusableCell(withIdentifier: "CodeCells") as? OperatorCodeTableViewCell

let sfMonoDescriptor = UIFont.systemFont(ofSize: 15).fontDescriptor.withDesign(.monospaced)
let sfMonoFont = UIFont(descriptor: sfMonoDescriptor!, size: 15)
code?.textLabel?.font = sfMonoFont
code?.textLabel?.textColor = UIColor.white
code?.textLabel?.text = """
1.  var str = \"Hello World!\"
"""
code?.backgroundColor =  #colorLiteral(red: 0.1307941079, green: 0.1277202666, blue: 0.1972439587, alpha: 1)
code?.textLabel?.numberOfLines = 0
code?.textLabel?.lineBreakMode = .byWordWrapping
code?.textLabel?.textAlignment = .left

// MARK: Nsattributedstring
let attributedCoding = NSMutableAttributedString(string: (code?.textLabel!.text)!)

let oneCodeconecpt = ((code?.textLabel!.text)! as NSString).range(of: "var")
attributedCoding.addAttribute(NSAttributedString.Key.foregroundColor, value: systemPink, range: oneCodeconecpt)

let oneCodeconecptTwo = ((code?.textLabel!.text)! as NSString).range(of: "\"Hello World\"")
attributedCoding.addAttribute(NSAttributedString.Key.foregroundColor, value: systemRed , range: oneCodeconecptTwo)

code?.textLabel?.attributedText = attributedCoding

return code!

}

现在,我注意到执行应用后,字符串在textLabel中没有突出显示。

屏幕截图:https://ibb.co/zNQnB74

让我知道您是否知道这些问题在带有NSAttributedString的textLabel中引起了什么?

swift uitableview nsattributedstring textlabel
1个回答
0
投票

这里的问题是,您(偶然地)没有在搜索!的范围内包括惊叹号"Hello World!"。在使用UILabel给出的示例中,您没有在!]中包含"Hello World"

在这种情况下,从较小的子字符串构建NSMutableAttributedString可能会更容易。这将防止您在搜索子字符串的范围和应用属性时出错。

尝试这样的事情:

let attr = NSMutableAttributedString(string: "1.   ")
attr.append(NSAttributedString(string: "var", attributes: [.foregroundColor: UIColor.systemPink]))
attr.append(NSAttributedString(string: " str = "))
attr.append(NSAttributedString(string: "\"Hello World!\"", attributes: [.foregroundColor: UIColor.systemRed]))

可以说,这有点难于理解,但是除非您要构建一个解析器来为您突出显示代码,否则实际上并不需要(而且不易出错)进行所有字符串搜索以突出显示一个字符串。几个部分。

© www.soinside.com 2019 - 2024. All rights reserved.