如何使用 NSAttributedString 强制 hr 分隔符填充请求的宽度?

问题描述 投票:0回答:1

这就是我定义分隔符的方式:

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)

结果如下:

enter image description here

如何将该线扩展到 300 像素?

ios swift nsattributedstring
1个回答
0
投票

给定宽度的答案等于

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)
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.