Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString boundingRectWithSize:options:attributes:context: not usable in Swift?

Tags:

swift

I get the error...

Could not find an overload for 'init' that accepts the supplied arguments

...when I try to use...

extension UIFont {
  func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
    NSString(string).boundingRectWithSize(CGSize(width, DBL_MAX),
                                          options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                                          attributes: [NSFontAttributeName: self],
                                          context: nil).size
  }
}

Does NSString not support this method anymore, or am I messing up on the syntax?

like image 493
aleclarson Avatar asked Jun 08 '14 14:06

aleclarson


4 Answers

The initializers expect named arguments.

extension UIFont {
    func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
        return NSString(string: string).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
            options: NSStringDrawingOptions.UsesLineFragmentOrigin,
            attributes: [NSFontAttributeName: self],
            context: nil).size
    }
}

Note: Strings can be cast to NSStrings.

extension UIFont {
    func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
        return (string as NSString).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
            options: NSStringDrawingOptions.UsesLineFragmentOrigin,
            attributes: [NSFontAttributeName: self],
            context: nil).size
    }
}

or

extension UIFont {
    func sizeOfString (string: NSString, constrainedToWidth width: Double) -> CGSize {
        return string.boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
            options: NSStringDrawingOptions.UsesLineFragmentOrigin,
            attributes: [NSFontAttributeName: self],
            context: nil).size
    }
}

--

UPDATED

For Swift 4 syntax

extension UIFont {
    func sizeOfString (string: String, constrainedToWidth width: Double) -> CGSize {
        return NSString(string: string).boundingRect(
            with: CGSize(width: width, height: .greatestFiniteMagnitude),
            options: .usesLineFragmentOrigin,
            attributes: [.font: self],
            context: nil).size
    }
}
like image 93
Jeffery Thomas Avatar answered Nov 19 '22 08:11

Jeffery Thomas


Alternatively you could cast it into an NSString

if let ns_str:NSString = str as NSString? {

   let sizeOfString = ns_str.boundingRectWithSize(
                                 CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity), 
                                 options: NSStringDrawingOptions.UsesLineFragmentOrigin, 
                                 attributes: [NSFontAttributeName: lbl.font], 
                                 context: nil).size
}
like image 28
Mike M Avatar answered Nov 19 '22 09:11

Mike M


Latest Swift:

import UIKit

extension UIFont {
    func sizeOfString(string: String, constrainedToWidth width: Double) -> CGSize {
        return NSString(string: string).boundingRect(with: CGSize(width: width, height: .greatestFiniteMagnitude),
                                                             options: .usesLineFragmentOrigin,
                                                             attributes: [NSFontAttributeName: self],
                                                             context: nil).size
    }
}
like image 1
user3246173 Avatar answered Nov 19 '22 07:11

user3246173


Latest Swift

func sizeOfString (string: String, constrainedToHeight height: Double) -> CGSize {
    return NSString(string: string).boundingRect(with: CGSize(width: DBL_MAX, height: height),
                                                 options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                                 attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)],
                                                 context: nil).size
}
like image 4
Amit Shekhar Avatar answered Nov 19 '22 07:11

Amit Shekhar