首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查NSUnderlineStyle BitMask的值

检查NSUnderlineStyle BitMask的值
EN

Stack Overflow用户
提问于 2016-07-31 21:05:14
回答 1查看 484关注 0票数 1

我希望能够正确地读取Swift中的NSUnderlineStyle值。比看上去复杂多了。

首先,快速回顾一下。NSUnderlineStyle是具有以下值的枚举:

代码语言:javascript
复制
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,

NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,

NSUnderlineByWord = 0x8000 

有三组不同的选项,根据苹果的文档,“样式、模式和可选的逐字掩码一起生成NSUnderlineStyleAttributeNameNSStrikethroughStyleAttributeName的值”。

NSUnderlineStyle 不实现新的OptionSet协议,这意味着值必须与整数位操作相结合(很明显)。如果是这样的话,就有可能出现类似于以下情况的情况:

代码语言:javascript
复制
let operation: NSDragOperation = [.copy, .move]
if operation.contains(.copy) {
  // Yay!
}

但遗憾的是,这行不通。

我的理解是,要正确检查带有数字的位标志,如果B位于A中,则为(A &B == B)。

,但是,这似乎不适用于NSUnderlineStyle,我不知道为什么。我不是口罩专家。

下面是演示问题的示例代码。

代码语言:javascript
复制
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")
}

该守则应产生:

代码语言:javascript
复制
style single found
pattern dash dot dot found
by word flag found

但是却产生了:

代码语言:javascript
复制
style single found
pattern solid found
pattern dash dot dot found
by word flag found

在其他情况下,这是失败的。

怎样才能正确地阅读下划线样式?

任何帮助都将不胜感激。而且,如果有人对失败的原因有任何洞察力,那就太棒了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-31 21:31:58

“样式”和“模式”的枚举值不是位标志。例如,NSUnderlineStyleNoneNSUnderlinePatternSolid的值都是零,而NSUnderlineStyleSingleNSUnderlineStyleDouble有一些共同点(0x01)。

您必须提取与每个组对应的位(样式、模式、标志),并将该值与可能的枚举值进行比较,对每个组分别进行比较。

不幸的是,苹果并没有为每一组提供比特掩码(据我所见),所以我们定义我们自己的。样式值(0x00、0x01、0x02、0x09)都使用位0.3,模式值(0x0、0x100、0x200、0x300、0x400)都使用位8.11。这表明了以下定义:

代码语言:javascript
复制
let underlineStyleMask   = 0x000F
let underlinePatternMask = 0x0F00

然后可以用作

代码语言:javascript
复制
// Check style:
switch style & underlineStyleMask {
case NSUnderlineStyle.styleNone.rawValue:
    NSLog("no style")
case NSUnderlineStyle.styleSingle.rawValue:
    NSLog("single style")
case NSUnderlineStyle.styleDouble.rawValue:
    NSLog("double style")
case NSUnderlineStyle.styleThick.rawValue:
    NSLog("thick style")
default:
    NSLog("unknown style")
}

// Check pattern:
switch style & underlinePatternMask {
case NSUnderlineStyle.patternSolid.rawValue:
    NSLog("solid pattern")
case NSUnderlineStyle.patternDot.rawValue:
    NSLog("dot pattern")
case NSUnderlineStyle.patternDash.rawValue:
    NSLog("dash pattern")
case NSUnderlineStyle.patternDashDot.rawValue:
    NSLog("dash dot pattern")
case NSUnderlineStyle.patternDashDotDot.rawValue:
    NSLog("dash dot dot pattern")
default:
    NSLog("unknown pattern")
}

// Check flags:
if style & NSUnderlineStyle.byWord.rawValue != 0 {
    NSLog("by word flag")
}

但请注意,如果苹果在未来增加更多的定义,这可能会中断,例如

代码语言:javascript
复制
NSUnderlineStyleStrange = 0x22

会被错误地检测为NSUnderlineStyleThick

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38688387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档