如何在swift中编辑多行标题样式?我可以让#34;下周"大胆和日期定期?另一个问题是如何在标题的底部添加一些填充?
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if section == 0{
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
header.textLabel?.font = UIFont(name: "Helvetica", size: 18)
header.textLabel?.text = "Next Week \n \(firstDay) - \(seventhDay)"
header.textLabel?.frame = header.frame
header.textLabel?.textAlignment = NSTextAlignment.center
}
}
当前代码
答案 0 :(得分:0)
要使字符串的一部分变为粗体并使其他部分保持规则,您可以使用NS(Mutable)AttributedString
。这允许您为字符串的特定部分设置所有不同类型的样式:
let header = UITableViewHeaderFooterView()
header.textLabel?.textColor = UIColor.white
var mutableString = NSMutableAttributedString(string: "Next Week \n \(firstDay) - \(seventhDay)", attributes: [NSFontAttributeName:UIFont(name: "Helvetica", size:18.0)!])
mutableString.addAttributes([NSFontAttributeName:UIFont(name:"Helvetica-Bold", size:18.0)!], range: NSRange(location: 0, length: 9)) //lenght 9 because "Next Week" is 9 characters
header.textLabel?.attributedText = mutableString
然后添加填充只需使textLabel的高度小于标题的高度,但使它们从相同的高度开始。
header.textLabel?.frame = CGRect(x: 0, y: 0, width: header.frame.width, height: header.frame.height-20) //minX and minY because the screen renders from top left
header.textLabel?.textAlignment = NSTextAlignment.center