我有一些真正被诅咒的代码,通过检查NSParagraphStyle字符串来检测一个style.description实例是否有非空的“列表”:
attributedString.enumerateAttribute(
.paragraphStyle,
in: fullLength,
options: .longestEffectiveRangeNotRequired
) { style, range, _ in
guard let style = style as? NSParagraphStyle else { return }
if let maybeList = style.description
.components(separatedBy: "Lists (\n")
.dropFirst().first,
!maybeList.starts(with: ")") {
// we have (part of) a list-item! ... but I feel dirty
}这适用于描述字符串,如此字符串:
Alignment 0, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 36, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (
11L,
36N
), DefaultTabInterval 36, Blocks (null), Lists (
"NSTextList 0x6000014408d0 format <{disc}>"
), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint '(null)' 但这不是我所称的“健壮”:-) -- docs确实提到了一个textLists属性,但它只在AppKit (macOS)中,而我正在为UIKit (iOS)构建。不过,从描述字符串的角度来看,它确实看起来就像在那里。所以:我能“合法地”说出来吗?
发布于 2021-12-03 18:18:20
在编写本报告时,没有使用(NS)AttributedString创建列表的公共API。要列出自己的列表,您需要使用像headIndent这样的技巧,就像这篇文章展示的那样。
但是,NSParagraphStyle仍然响应textLists选择器(至少目前是这样),尽管它没有像在AppKit中那样将textLists公开为属性。
因此,从技术上讲,您可以:
extension NSParagraphStyle {
var containsLists: Bool {
let sel: Selector = "textLists"
guard self.responds(to: sel) else { return false }
guard let list = self.perform(sel).takeUnretainedValue() as? [Any] else { return false }
return !list.isEmpty
}
}但我不确定这是否会让你的应用程序从应用商店被拒绝。
https://stackoverflow.com/questions/70218262
复制相似问题