Is it possible to get a listing of attributes and ranges for an NSMutableAttributedString?

房东的猫 提交于 2019-12-02 21:39:24

Apple expects you to use enumerateAttributesInRange:options:usingBlock:. The block you supply will receive ranges and the attributes applicable for that range.

I've used that in my code to create invisible buttons that are placed behind text so that it acts as a hyperlink.

You could also use enumerateAttribute:inRange:options:usingBlock: if there's only one you're interested in, but no halfway house is provided where you might be interested in, say, two attributes but not every attribute.

Swift 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)
}

In case, that you have defined attributedText of label.

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))

If you need all of the attributes from the string in the entire range, use this code:

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

Apple's Docs have a number of methods to access attributes:

To retrieve attribute values from either type of attributed string, use any of these methods:

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

Swift 3:

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

Swift 4:

If you want to get the attributes for NSTextAttachment (just change the attribute value if you want font)

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)")
                            }
}

I have modified one of the answers with suggested fixes to prevent infinite recursion and application crashes.

@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!])
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!