![](/img/trans.png)
[英]swift change section array element text or font color in tableview header
[英]how to change color of the part of header title in tableView in swift 3?
我讀過類似的問題,但是我沒有答案:我有帶標題的表格視圖,第一個有標題和數字,我想在表格視圖的標題中更改該數字的顏色,這是我的代碼:
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是另一個變量,我無法使用類似的問題
如果要更改字符串部分的顏色,則需要使用NS(Mutable)AttributedString
並將color屬性僅添加到特定范圍:
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
我可能無法完全理解您的問題,但我想您要為第一個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
}
}
您可以通過編寫以下代碼來實現:
我創建了一個通用函數以在每個控制器中重用它。
//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)
您也可以使用它來更改特定字符的顏色。 您需要指定字符串長度和字符的起始位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.