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

Ios 如何在swift 3中更改tableView中标题部分的颜色?,ios,uitableview,swift3,textcolor,Ios,Uitableview,Swift3,Textcolor,我读过类似的问题,但没有得到答案: 我有一个带有标题的表视图,第一个有标题和数字,我想在表视图的标题中更改该数字的颜色 这是我的密码: var headerList = ["Account" , "Help" , "" ] override func viewDidLoad() { super.viewDidLoad() self.headerList[0] = "Account \(userAccountMoney)" } override func tableVi

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

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是另一个变量,我无法使用类似的问题,我可能无法完全理解您的问题,但我认为您希望设置不同的名称第一个tableview标题的颜色。 您希望通过在
部分
变量上使用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
}
 }

我可能不完全理解您的问题,但我认为您希望为第一个tableview标题设置不同的颜色。 您希望通过在
部分
变量上使用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(可变)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”


但是,在您的情况下,我建议将
标题列表创建为常量

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]
}

如果要更改字符串部分的颜色,需要使用
NS(可变)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”


但是,在您的情况下,我建议将
标题列表创建为常量

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

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

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

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

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

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


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

创建一个
NSAttributedString
并使用属性
attributedText
我已经阅读了类似的问题,但问题是我想更改文本颜色,但不是第一个标题
NSAttributedString
中的所有单词都是解决方案查看我的答案。创建一个
NSAttributedString
并使用属性
attributedText
我读过类似的问题,但问题是我想更改文本颜色,但不是第一个标题
NSAttributedString
中的所有单词都是解决方案。查看我的答案。我不想更改第0节中的所有文本颜色我只想更改第0节中的一个单词的文本颜色现在请检查。我添加了一个新方法,在这个方法中,你可以传递你的字符串和一个你想要改变颜色的文本范围。如果您仍然面临任何问题,请告诉我我不想更改第0节中的所有文本颜色我只想更改第0节中的一个单词的文本颜色现在请检查。我添加了一个新方法,在这个方法中,你可以传递你的字符串和一个你想要改变颜色的文本范围。如果您仍然面临任何问题,请告诉我由于未捕获的异常“NSRangeException”,使用此代码终止应用程序时我收到错误,原因是:“NSMutableRLEARY objectAtIndex:effectiveRange::Out bounds”***第一次抛出调用堆栈:然后在更改
viewDidLoad()
中的节头之前重新加载表视图。我通过检查更新了答案。由于未捕获的异常“NSRangeException”,使用此代码终止应用程序时出现lotI Receive错误,工作正常,原因是:“NSMutableRLEARY objectAtIndex:effectiveRange::Out bounds”***第一次抛出调用堆栈:然后在更改
viewDidLoad()中的节标题之前重新加载表视图
。我用支票更新了答案。效果很好,非常感谢