Search code examples
iosswiftswift3nsattributedstring

NSRangePointer error Swift


This is what I am trying to achieve:

var range : NSRange? = NSMakeRange(0, (attributedString?.length)!)
attributedString?.attribute(NSFontAttributeName: UIFont.fontNames(forFamilyName: "OpenSans-Regular"), at: 1, effectiveRange: &range) 

In second line, I receive a compile time error:

Argument labels '(NSFontAttributeName:, at:, effectiveRange:)' do not match any available overloads


Solution

  • You need:

    var range = NSRange(location: 0, length: attributedString?.length)
    let font = attributedString?.attribute(NSFontAttributeName, at: 1, effectiveRange: &range)
    

    The 1st parameter is just the attribute you wish to check for. You don't pass in a specific font. This method will tell you want font is found at the specified location.