【问题标题】:Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?是否可以获得 NSMutableAttributedString 的属性和范围列表?
【发布时间】:2013-11-07 18:59:20
【问题描述】:

我创建了一个采用 NSAttributedString 的方法,我希望动态创建一个子视图和标签以将字符串放入其中。因为需要确定字体和大小等属性才能正确确定标签的大小,所以我需要确定是否可以遍历已应用于属性字符串的值和范围?

我知道我可以单独传递属性,但为了可重用性,我希望能够向方法传递尽可能少的参数。

【问题讨论】:

  • 简单看一下文档就会发现enumerateAttributesInRange:options:usingBlock:

标签: ios uilabel nsattributedstring nsmutableattributedstring


【解决方案1】:

斯威夫特 5 – 4

let attributedText = NSAttributedString(string: "Hello, playground", attributes: [
  .foregroundColor: UIColor.red, 
  .backgroundColor: UIColor.green, 
  .ligature: 1, 
  .strikethroughStyle: 1
])

// retrieve attributes
let attributes = attributedText.attributes(at: 0, effectiveRange: nil)

// iterate each attribute
for attr in attributes {
  print(attr.key, attr.value)
}

如果你定义了attributedText的标签。

斯威夫特 3

var attributes = attributedText.attributes(
  at: 0, 
  longestEffectiveRange: nil, 
  in: NSRange(location: 0, length: attributedText.length)
)

斯威夫特 2.2

var attributes = attributedText.attributesAtIndex(0,   
  longestEffectiveRange: nil, 
  inRange: NSMakeRange(0, attributedText.length)
)

【讨论】:

  • 你能解释一下为什么effectiveRange参数在你的例子中得到nil参数吗?为什么不应该将其设置为字符串中字符的总范围?谢谢。
  • @MaxDesiatov 如果您阅读文档,它会声明“返回时,属性和值与索引处相同的范围。这个范围不一定是覆盖的最大范围,其范围取决于实现。如果需要最大范围,请使用属性(at:longestEffectiveRange:in:)。如果不需要此值,请传递 NULL"。我们不需要这个值,所以我们不捕获它。
【解决方案2】:

Apple 希望您使用enumerateAttributesInRange:options:usingBlock:。您提供的块将接收范围和适用于该范围的属性。

我在我的代码中使用它来创建放置在文本后面的不可见按钮,以便它充当超链接。

如果只有一个您感兴趣,您也可以使用enumerateAttribute:inRange:options:usingBlock:,但没有提供您可能感兴趣的中途站,例如,两个属性但不是每个属性。

【讨论】:

    【解决方案3】:

    如果您需要整个范围内字符串的所有属性,请使用以下代码:

    NSDictionary *attributesFromString = [stringWithAttributes attributesAtIndex:0 longestEffectiveRange:nil inRange:NSMakeRange(0, stringWithAttributes.length)];

    【讨论】:

      【解决方案4】:

      斯威夫特 3:

      // in case, that you have defined `attributedText` of label
      var attributes = attributedText.attributes(at: 0, longestEffectiveRange: nil, in: NSMakeRange(0, attributedText.length))
      ...
      

      【讨论】:

        【解决方案5】:

        Apple's Docs 有多种访问属性的方法:

        要从任一类型的属性字符串中检索属性值,请使用以下任一方法:

        attributesAtIndex:effectiveRange: attributesAtIndex:longestEffectiveRange:inRange: attribute:atIndex:effectiveRange: attribute:atIndex:longestEffectiveRange:inRange: fontAttributesInRange: rulerAttributesInRange:

        【讨论】:

          【解决方案6】:

          斯威夫特 4:

          如果你想获取 NSTextAttachment 的属性(如果你想要字体,只需更改属性值)

          commentTextView.attributedText.enumerateAttribute(NSAttachmentAttributeName,
                                  in:NSMakeRange(0, commentTextView.attributedText.length),
                                  options:.longestEffectiveRangeNotRequired) {
                                  value, range, stop in
                                      if let attachment = value as? NSTextAttachment {
                                          print("GOT AN ATTACHMENT! for comment at \(indexPath.row)")
                                      }
          }
          

          【讨论】:

            【解决方案7】:

            我已通过建议的修复修改了其中一个答案,以防止无限递归和应用程序崩溃。

            @IBDesignable
            extension UITextField{
            
                @IBInspectable var placeHolderColor: UIColor? {
                    get {
                        if let color = self.attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
                            return color
                        }
                        return nil
                    }
                    set {
                        self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[.foregroundColor: newValue!])
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2013-05-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-12-17
              • 2017-07-11
              • 2012-07-22
              相关资源
              最近更新 更多