1

我有一个带有 UITextKit 样式(NSStrikethroughStyleAttributeName)的 UITextView 和 TextKit:

在此处输入图像描述

这是我的代码:

@IBOutlet weak var textView: UITextView!
var dict = [String: AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let newFont = UIFont(name:"HelveticaNeue", size: textView.font!.pointSize)
    self.textView.font = newFont
    dict[NSFontAttributeName] = newFont
    
    let selectedRange: NSRange  = NSMakeRange(12,6)
    self.makeStrikeThrough(selectedRange)
    
}

func makeStrikeThrough(selectedRange: NSRange) {
    dict[NSStrikethroughStyleAttributeName] = 2
    self.textView.textStorage.beginEditing()
    self.textView.textStorage.setAttributes(dict, range: selectedRange)
    self.textView.textStorage.endEditing()
}

现在我必须有一种方法来检测这个字体属性。有什么方法可以获取信息:

在 selectedRange: NSRange 12, 6 我使用属性NSStrikethroughStyleAttributeName属性2,也许作为一个数组条目???

欢迎任何想法!

4

1 回答 1

3

唯一属性范围的离散化(这些范围的左/右相邻范围)

您可以使用重复使用的attributesAtIndex(location:effectiveRange:)方法NSAttributedString将整个属性字符串的范围编码为子范围列表,每个子范围都包含一组属性和值。

宣言:

func attributesAtIndex(location: Int, 
     effectiveRange range: NSRangePointer) -> [String : AnyObject]

描述:

返回给定索引处字符的属性。

返回值:

处角色的属性index

更具体地说,用于attributesAtIndex(...)创建NSAttributedString返回和元组数组的扩展,元组定义为

  • 第一个元组元素是一组不同属性(或缺少属性)的范围。
  • 第二个元组元素本身就是一个元组数组,这些元组定义为:
    • 第一个子元组元素是属性名称。
    • 第二个子元组元素是属性值。
  • 或者,第二个元组元素是一个[String: AnyObject]数组,对应于属性及其各自的值(包括下面的两种变体)

由多个属性赋予的属性字符串中的范围自然会返回一个包含多个元素的内部元组数组,而根本没有属性的范围将返回一个空的内部元组数组。

扩展名(s, 两种选择)如下:

/* let 2nd tuple be an array of tuples itself */
extension NSAttributedString {
    func getAttributes() -> [(NSRange, [(String, AnyObject)])] {
        var attributesOverRanges : [(NSRange, [(String, AnyObject)])] = []
        var rng = NSRange()
        var idx = 0

        while idx < self.length {
            let foo = self.attributesAtIndex(idx, effectiveRange: &rng)
            var attributes : [(String, AnyObject)] = []

            for (k, v) in foo { attributes.append(k, v) }
            attributesOverRanges.append((rng, attributes))

            idx = max(idx + 1, rng.toRange()?.endIndex ?? 0)
        }
        return attributesOverRanges
    }
}

/* or, let 2nd tuple be a [String: AnyObject] dictionary */
extension NSAttributedString {
    func getAttributes() -> [(NSRange, [String: AnyObject])] {
        var attributesOverRanges : [(NSRange, [String: AnyObject])] = []
        var rng = NSRange()
        var idx = 0

        while idx < self.length {
            let foo = self.attributesAtIndex(idx, effectiveRange: &rng)
            attributesOverRanges.append((rng, foo))

            idx = max(idx + 1, rng.toRange()?.endIndex ?? 0)
        }
        return attributesOverRanges
    }
}

示例用法:

/* Example setup */
let fooString = "foo foo foo foo foo foo foo"
var fooAttrString = NSMutableAttributedString(string: fooString)
let selectedRange: NSRange = NSMakeRange(12,6)

// attr1: strikethrough over range (12,6) (12..<18)
var myRange = NSRange(location: 12, length: 6)
let strikeThroughAttr = [ NSStrikethroughStyleAttributeName: 2 ]
fooAttrString.addAttributes(strikeThroughAttr, range: myRange)

// attr2: font over range (16,8) (16..<24)
myRange = NSRange(location: 16, length: 8)
let fontAttr = [ NSFontAttributeName: UIFont(name:"HelveticaNeue", size: 20)! ]
fooAttrString.addAttributes(fontAttr, range: myRange)

/* Example usage: extension */
let attributesOverRanges = fooAttrString.getAttributes()
for (rng, attributes) in attributesOverRanges {
    print("Attributes over range \(rng):")
    attributes.forEach { print("\t\($0.0) = \($0.1)") }
}
/* Attributes over range (0,12):
   Attributes over range (12,4):
       NSStrikethrough = 2
   Attributes over range (16,2):
       NSFont = <UICTFont: 0x7fcfd860f0b0> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 20.00pt
       NSStrikethrough = 2
   Attributes over range (18,6):
       NSFont = <UICTFont: 0x7fcfd860f0b0> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 20.00pt
   Attributes over range (24,3):                       */

将上述内容应用于您的UITextView实例,特别是属性textStorage

现在, 的textStorage属性UITextView是 类型NSTextStorage,它是 的(半具体的)子类NSMutableAttributedString,它本身是 的子类NSAttributedString。因此,上面的扩展getAttributes()将可以访问并且在NSTextStorage实例上也可以正常工作,例如textView.textStorage在您的问题中。

因此,使用与上述相同的扩展,我们设置了一个类似的示例,但用于UITextView具有属性的textStorage属性。

/* Example setup: UITextView:s 'textStorage' (type NSTextStorage) */
let fooString = "foo foo foo foo foo foo foo"

// attr1: strikethrough over range (12,6) (12..<18)
let strikeThroughRng = NSRange(location: 12, length: 6)
let strikeThroughAttr = [ NSStrikethroughStyleAttributeName: 2 ]

// attr2: font over range (16,8) (16..<24)
let fontRng = NSRange(location: 16, length: 8)
let fontAttr = [ NSFontAttributeName: UIFont(name:"HelveticaNeue", size: 20)! ]

// create text view and set attributes
let textView = UITextView()
textView.text = fooString
textView.textStorage.beginEditing()
textView.textStorage.addAttributes(strikeThroughAttr, range: strikeThroughRng)
textView.textStorage.addAttributes(fontAttr, range: fontRng)
textView.textStorage.endEditing()

示例用法,扩展:

/* Example usage: extension (uses first version above) */
let attributesOverRanges = textView.textStorage.getAttributes()
for (rng, attributes) in attributesOverRanges {
    print("Attributes over range \(rng):")
    attributes.forEach { print("\t\($0.0) = \($0.1)") }
}
/* Attributes over range (0,12):
       NSOriginalFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
       NSFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
   Attributes over range (12,4):
       NSOriginalFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
       NSFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
       NSStrikethrough = 2
   Attributes over range (16,2):
       NSFont = <UICTFont: 0x7ff610d80820> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 20.00pt
       NSStrikethrough = 2
   Attributes over range (18,6):
       NSFont = <UICTFont: 0x7ff610d80820> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 20.00pt
   Attributes over range (24,3):
       NSOriginalFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt
       NSFont = <UICTFont: 0x7ff610d88e20> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12.00pt */

NSAttributedString正如预期的那样,我们看到与上面示例相同的结果,不同之处在于textView.textStorage包含一些默认属性(NSFont, NSOriginalFont)。


搜索属性字符串以查找给定属性和属性值的首次出现

如果您愿意,您还可以编写一个扩展来搜索属性字符串以查找特定属性和值,使用attribute(attrName:atIndex:effectiveRange:)方法NSAttributedString

宣言:

func attribute(attrName: String, atIndex location: Int, 
               effectiveRange range: NSRangePointer) -> AnyObject?

描述:

返回给定索引处具有给定字符名称的属性的值,并通过引用该属性的应用范围。

返回值:

attributeName以处的字符 命名的属性的值index,或者nil如果没有这样的属性。

更具体地说,创建一个扩展

  • 在属性字符串中搜索给NSFontAttributeName定值(例如)的给定属性(例如2),并返回NSRange给定属性字符串中此类属性部分第一次出现的范围( )nil,如果找不到,则返回范围( )。

NSAttributedString扩展如下

/* find the range of (the first occurence of) a given 
   attribute 'attrName' for a given value 'forValue'. */
extension NSAttributedString {

    func findRangeOfAttribute(attrName: String, forValue value: AnyObject) -> NSRange? {

        var rng = NSRange()

        /* Is attribute (with given value) in range 0...X ? */
        if let val = self.attribute(attrName, atIndex: 0, effectiveRange: &rng) where val.isEqual(value) { return rng }

        /* If not, is attribute (with given value) anywhere in range X+1..<end? */
        else if
            let from = rng.toRange()?.endIndex where from < self.length - 1,
            let val = self.attribute(attrName, atIndex: from, effectiveRange: &rng) where val.isEqual(value) { return rng }

        /* if none of the above, return nil */
        return nil
    }
}

示例用法:

/* Example */
let fooString = "foo foo foo foo foo foo foo"
var fooAttrString = NSMutableAttributedString(string: fooString)
let selectedRange: NSRange  = NSMakeRange(12,6)

let myRange = NSRange(location: 12, length: 6)
let attr = [ NSStrikethroughStyleAttributeName: 2 ]
fooAttrString.addAttributes(attr, range: myRange)

/* Example usage: extension */
if let rngOfFirstStrikethrough = fooAttrString.findRangeOfAttribute(NSStrikethroughStyleAttributeName, forValue: 2) {
    print(rngOfStrikethrough) // (12,6)
}
于 2016-04-03T01:01:15.717 回答