我想的值,以便能夠正確讀取斯威夫特的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 .:看到更新的答案。 –