这就是我定义分隔符的方式:
extension NSAttributedString {
static var separator: NSAttributedString {
let attributes: [NSAttributedString.Key: Any] = [
.strikethroughStyle: NSUnderlineStyle.single.rawValue,
.strikethroughColor: UIColor.lightGray,
]
return NSAttributedString(string: "\n\r\u{00A0} \u{0009} \u{00A0}\n\n", attributes: attributes)
}
}
并像这样使用它:
attributed.append(.separator)
结果如下:
如何将该线扩展到 300 像素?
给定宽度的答案等于
300
。分隔符也位于屏幕中央。
static var separator: NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.alignment = .center
paragraphStyle.tabStops = [NSTextTab(textAlignment: .center, location: 300, options: [:])] //here you can define the width of separator
let attributes: [NSAttributedString.Key: Any] = [
.strikethroughStyle: NSUnderlineStyle.single.rawValue,
.strikethroughColor: UIColor.mineShaft.alpha(0.3),
.paragraphStyle: paragraphStyle,
]
return NSAttributedString(string: "\n\u{00a0}\t\n", attributes: attributes)
}