如何在 swift 3 的 tableView 中更改 header 标题部分的颜色?

how to change color of the part of header title in tableView in swift 3?

我看过类似的问题但是我没有得到答案: 我有 Table 使用 Headers 查看,第一个有标题和数字 我想在 table 视图的 header 中更改该数字的颜色 这是我的代码:

var headerList = ["Account" , "Help" , "" ]

override func viewDidLoad() {
         super.viewDidLoad()
self.headerList[0] = "Account \(userAccountMoney)"

 }
  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
    returnedView.backgroundColor = UIColor.init(red: 229/255, green: 233/255, blue: 236/255, alpha: 1.0)

    let label = UILabel(frame: CGRect(x: -20, y: 7, width: view.frame.size.width, height: 25))
    label.text = self.headerList[section]
    label.textAlignment = .right
    label.textColor = .lightGray
 }

正如您在我的代码中看到的那样,header 标题是灰色的,但我想在第一个 header 标题中,帐户字样仍然是灰色的,但是 userAccountMoney 是绿色的问题是因为 userAccountMoney是另一个变量我不能使用类似的问题

我可能不完全理解你的问题,但我认为你想为你的第一个表格视图设置不同的颜色 header。 你想通过在 section 变量上使用 If-else 语句来做到这一点。 像这样

func setAttributedString(text string : String, subStringRange range : NSRange, subStringFont font1 : UIFont, mainStringFont font2 : UIFont, subStringColor color1 : UIColor = UIColor.gray, mainStringColor color2 : UIColor = UIColor.green) -> NSAttributedString {

            let mainStringAttribute = [ NSFontAttributeName: font2, NSForegroundColorAttributeName : color2] as [String : Any]
            let attributedString = NSMutableAttributedString(string: string, attributes: mainStringAttribute)

            let subStringAttribute = [ NSFontAttributeName: font1, NSForegroundColorAttributeName : color1] as [String : Any]
            attributedString.addAttributes(subStringAttribute, range: range)

            return attributedString
        }

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

     if (section == 0) // this is first header
     { 

    let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
    returnedView.backgroundColor = UIColor.init(red: 229/255, green: 233/255, blue: 236/255, alpha: 1.0)

    let label = UILabel(frame: CGRect(x: -20, y: 7, width: view.frame.size.width, height: 25))
    label.text = self.headerList[section]
    label.textAlignment = .right
        let font1 = UIFont.systemFont(ofSize: 14.0)
        let font2 = UIFont.systemFont(ofSize: 14.0)

        label.attributedText = setAttributedString(text: "", subStringRange: NSRange(location: 0, length: 1), subStringFont: font1, mainStringFont: font2)


  } 
   else /// remaining headers
   {

 let returnedView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 25))
    returnedView.backgroundColor = UIColor.init(red: 229/255, green: 233/255, blue: 236/255, alpha: 1.0)

    let label = UILabel(frame: CGRect(x: -20, y: 7, width: view.frame.size.width, height: 25))
    label.text = self.headerList[section]
    label.textAlignment = .right
    label.textColor = .lightGray
}
 }

如果你想改变字符串的一部分的颜色,你需要使用NS(Mutable)AttributedString并且只在特定范围内添加颜色属性:

if section == 0 {
    let sectionTitle = self.headerList[0]
    let attributedString = NSMutableAttributedString(string: sectionTitle)
    if sectionTitle.characters.count > 8 {
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: 8, length: sectionTitle.characters.count - 8))
    }
    label.attributedText = attributedString
} else {
    label.text = self.headerList[section]
}

索引8是"Account "

之后的硬编码索引

但是在你的情况下我建议创建 headerList 作为常量

let headerList = ["" , "Help" , "" ]

然后删除viewDidLoad中的代码,使用此代码动态创建账户金额

if section == 0 {
    let attributedString = NSMutableAttributedString(string: "Account ")
    attributedString.append(NSAttributedString(string: "\(userAccountMoney)", attributes: [NSForegroundColorAttributeName : UIColor.red]))
    label.attributedText = attributedString
} else {
    label.text = self.headerList[section]
}

使用 NSAttributedString 可以创建两个多色文本,甚至可以更改字体。这是片段:

let titleAtt = NSAttributedString(string: "Hello", attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!, NSForegroundColorAttributeName: UIColor.blue])
let numberAtt = NSAttributedString(string: "123", attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!, NSForegroundColorAttributeName: UIColor.red])

let combinationAtt = NSMutableAttributedString() 
combinationAtt.append(titleAtt)
combinationAtt.append(numberAtt)
label.attributedText = combinationAtt

你可以通过如下代码实现:

我创建了一个通用函数以在每个控制器中重用它。

//MARK:- Set title with two different color
func setTitle(title : String, loc : Int, len : Int) -> NSMutableAttributedString
{
    let myMutableString = NSMutableAttributedString(
        string: title,
        attributes: [:])

    myMutableString.addAttribute(
        NSForegroundColorAttributeName,
        value: UIColor(red: 29/255, green: 140/255, blue: 242/255, alpha: 1.0),
        range: NSRange(
            location:loc,
            length:len))

    return myMutableString
}

我正在使用这个功能,

对于同一个控制器你可以写,

self.lblTitle.attributedText = self.setTitle(title: "My Profile", loc: 3, len: 7)

您也可以使用它来改变特定字符的颜色。您需要指定字符串的长度和字符的起始位置。