NSAttributedString 2个字符串与新行的组合
NSAttributedString combination of 2 Strings with new line
我正在努力实现这种约会
在集合视图单元格内。
所以我在 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
如何在它们之间添加分隔线?
您可以添加 \n
日期。
let theDay = formatDay(fullString: "\(example.date)\n", fontSize: 13)
您需要设置 NSMutableParagraphStyle
才能使您的文字 center
。为此尝试像 this SO answer 一样,您需要进行一些小的更改以使其与 Swift 3 一起使用,并确保您有足够的 height
来显示 2 行。
我正在努力实现这种约会
在集合视图单元格内。 所以我在 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
如何在它们之间添加分隔线?
您可以添加 \n
日期。
let theDay = formatDay(fullString: "\(example.date)\n", fontSize: 13)
您需要设置 NSMutableParagraphStyle
才能使您的文字 center
。为此尝试像 this SO answer 一样,您需要进行一些小的更改以使其与 Swift 3 一起使用,并确保您有足够的 height
来显示 2 行。