在集合视图单元格内。 所以我有2个单独字符串的日期编号和月份。
var day = example.date
var month = example.month
使用下面的功能我可以更改它们的字体颜色等。
func formatMonth(fullString: String, fontSize: Double) -> NSMutableAttributedString
{
let range = (fullString as NSString).range(of: fullString)
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: fullString)
myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue-Bold", size: CGFloat(fontSize))!
, NSForegroundColorAttributeName : UIColor.red], range: range)
return myMutableString
}
func formatDay(fullString: String, fontSize: Double) -> NSMutableAttributedString
{
let range = (fullString as NSString).range(of: fullString)
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: fullString)
myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue", size: CGFloat(fontSize))!
, NSForegroundColorAttributeName : UIColor.black], range: range)
return myMutableString
}
变量变得像这样
let theMonth = formatMonth(fullString: example.month, fontSize: 15)
let theDay = formatDay(fullString: example.date, fontSize: 13)
然后我将它们结合起来
let combination = NSMutableAttributedString()
combination.append(theDay)
combination.append(theMonth)
最后我得到了文本的组合。
date.attributedText = combination
通过这种方法,我可以看到另一个8FEb
我怎样才能在它们之间添加一条断线?
答案 0 :(得分:5)
您可以在一天内添加\n
。
let theDay = formatDay(fullString: "\(example.date)\n", fontSize: 13)
您需要设置NSMutableParagraphStyle
来制作文字center
。尝试使用this SO answer,你需要做一些改动才能使它与Swift 3一起使用,并确保你有足够的height
来显示2行。