使用Swift,我该如何将UITableViewHeader中的文本设置为富文本字符串?
我尝试过以下方式:
我尝试过以下方式:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// Text Color/Font
let header = view as! UITableViewHeaderFooterView
let headerString = header.textLabel?.text!
let textArr = headerString!.componentsSeparatedByString(", ")
let attributesBold = [
NSFontAttributeName : UIFont.boldSystemFontOfSize(20), NSForegroundColorAttributeName : UIColor(red: 44.0/255.0, green: 50.0/255.0, blue: 75.0/255.0, alpha: 1.0)]
let attributesNorm = [
NSFontAttributeName : UIFont.systemFontOfSize(20), NSForegroundColorAttributeName : UIColor(red: 44.0/255.0, green: 50.0/255.0, blue: 75.0/255.0, alpha: 1.0)]
var fullText = NSMutableAttributedString(string: headerString!)
let count = textArr[0].characters.count + 2 + textArr[1].characters.count + 2 //total length of bold string
fullText.addAttributes(attributesBold, range: NSRange(location: 0, length: count))
fullText.addAttributes(attributesNorm, range: NSRange(location:count, length: textArr[2].characters.count))
header.textLabel?.attributedText = fullText
}
但是我的iPad在UITableView中只添加一个项目时就崩溃了,我从没有看到过标题。