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

时间:2017-05-16 11:18:33

标签: ios uitableview swift3 textcolor

我读过类似的问题,但我没有得到答案: 我有表格视图带标题,第一个有标题和数字我想在表格视图的标题中更改该数字的颜色 这是我的代码:

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
 }

正如你在我的代码中看到标题标题是灰色但我想在第一个标题标题帐户字仍然是灰色但userAccountMoney是绿色问题是因为userAccountMoney是另一个我无法使用的变量类似的问题

4 个答案:

答案 0 :(得分:1)

如果要更改字符串部分的颜色,则需要使用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]
}

答案 1 :(得分:1)

使用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

答案 2 :(得分:0)

我可能不完全理解你的问题,但我认为你想为你的第一个tableview标题设置不同的颜色。 您希望通过在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
}
 }

答案 3 :(得分:0)

您可以通过编写如下代码来实现它:

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

//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)

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

相关问题