【发布时间】:2015-07-28 22:26:29
【问题描述】:
使用 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
}
但是,一旦 UITableView 中有一个项目,我的 iPad 就会崩溃,而且我从来没有看到标题。
【问题讨论】: