是否可以获取NSMutableAttributedString的属性和范围列表?

问题描述 投票:36回答:7

我创建了一个采用NSAttributedString的方法,我正在寻找动态创建子视图和标签以将字符串放入。因为需要确定字体和大小等属性来正确确定标签的大小,我需要确定是否可以迭代已应用于属性字符串的值和范围?

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

ios uilabel nsattributedstring nsmutableattributedstring
7个回答
21
投票

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

我在我的代码中使用它来创建隐藏在文本后面的隐藏按钮,以便它充当超链接。

你也可以使用enumerateAttribute:inRange:options:usingBlock:,如果只有你感兴趣的那个,但没有提供你可能感兴趣的中途房子,比如两个属性,但不是每个属性。


55
投票

斯威夫特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

Swift 3

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

Swift 2.2

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

11
投票

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

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


3
投票

Apple's Docs有许多方法可以访问属性:

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

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


3
投票

斯威夫特3:

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

1
投票

斯威夫特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)")
                            }
}

0
投票

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

@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!])
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.