我想的值,以便能够正确读取斯威夫特的NSUnderlineStyle的值。 这似乎比看起来更复杂。检查的NSUnderlineStyle位掩码
首先,快速回顾。 NSUnderlineStyle是具有以下值的枚举:
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
有三个不同的选项组。根据Apple's documentation,“样式,图案,和任选地通过字掩码逻辑或运算,以产生用于NSUnderlineStyleAttributeName
和NSStrikethroughStyleAttributeName
值”。
NSUnderlineStyle 不会实现新的OptionSet Protocol,这意味着值必须与整数位操作(显然)结合使用。如果它这样做,类似于下面的东西是可能的:
let operation: NSDragOperation = [.copy, .move]
if operation.contains(.copy) {
// Yay!
}
但遗憾的是,这是行不通的。
我的理解是正确检查位标志与数字,它是(A &乙== B)如果B是A.
然而,这似乎并不为NSUnderlineStyle工作,我不知道为什么。我不是一个位面专家。
下面是演示问题的示例代码。
let style = NSUnderlineStyle.styleSingle.rawValue |
NSUnderlineStyle.patternDashDotDot.rawValue |
NSUnderlineStyle.byWord.rawValue
if style & NSUnderlineStyle.styleSingle.rawValue == NSUnderlineStyle.styleSingle.rawValue {
NSLog("style single found")
}
if style & NSUnderlineStyle.styleDouble.rawValue == NSUnderlineStyle.styleDouble.rawValue {
NSLog("style double found")
}
if style & NSUnderlineStyle.styleThick.rawValue == NSUnderlineStyle.styleThick.rawValue {
NSLog("style thick found")
}
if style & NSUnderlineStyle.patternSolid.rawValue == NSUnderlineStyle.patternSolid.rawValue {
NSLog("pattern solid found")
}
if style & NSUnderlineStyle.patternDash.rawValue == NSUnderlineStyle.patternDash.rawValue {
NSLog("pattern dash found")
}
if style & NSUnderlineStyle.patternDot.rawValue == NSUnderlineStyle.patternDot.rawValue {
NSLog("pattern dot found")
}
if style & NSUnderlineStyle.patternDashDot.rawValue == NSUnderlineStyle.patternDashDot.rawValue {
NSLog("pattern dash dot found")
}
if style & NSUnderlineStyle.patternDashDotDot.rawValue == NSUnderlineStyle.patternDashDotDot.rawValue {
NSLog("pattern dash dot dot found")
}
if style & NSUnderlineStyle.byWord.rawValue == NSUnderlineStyle.byWord.rawValue {
NSLog("by word flag found")
}
此代码应该产生:
style single found
pattern dash dot dot found
by word flag found
,而是产生:
style single found
pattern solid found
pattern dash dot dot found
by word flag found
还有其他一些情况下,这将失败。
我能做些什么,以适当读下划线样式?
任何帮助将不胜感激。此外,如果任何人有任何的洞察力,为什么这是失败的,那也将是了不起的。
为“风格”和“模式”
嗨马丁,谢谢你解释一个例子!这工作很好。你能解释一下,你怎么知道用0x0F00和0x000F掩盖它?我希望有一天能达到这个水平,并且我想知道你的思考过程。 –
@JosephE .:看到更新的答案。 –