53

In objective-C I was able to use:

    CGSize stringsize =
     [strLocalTelefone sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}];

But in Swift Language I didn't found any solution for this situation.

Any Help?

4
  • 3
    I can't find a solution to get the size of text in CGSize object... Jun 10, 2014 at 13:05
  • I want resize the button to it`s content text... Jun 10, 2014 at 13:06
  • @NaldoLopes, just an FYI, typically questions are not re-edited to include the solutions. Questions and answers are designed to be separate. By checking an answer it tells people what the solution you used was.
    – Firo
    Jun 10, 2014 at 13:25
  • ok...my bad... tanks for your help! Jun 10, 2014 at 13:27

6 Answers 6

159

what I did is something like this:

swift 5.x

let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [.font: UIFont.systemFont(ofSize: 14)])

swift 4.x

let myString = "Some text is just here..."
let size: CGSize = myString.size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])

swift 3

var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])

swift 2.x

var originalString: String = "Some text is just here..."
let myString: NSString = originalString as NSString
let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
15
  • @holex What if I am using Custom font?
    – Developer
    Nov 12, 2014 at 10:49
  • @Developer, obviously you need to use the custom font instead of the system font.
    – holex
    Nov 12, 2014 at 10:51
  • @holex I am new to Swift. I meant how will I write line of code for that with size?
    – Developer
    Nov 12, 2014 at 10:54
  • 2
    @Developer, the line let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 15.0)!]) works for me with no problem, I'm not sure where you have been stuck at.
    – holex
    Nov 12, 2014 at 11:32
  • 1
    @NavneetSharma, String in Swift fully supports unicode characters, so it should be no difference at all.
    – holex
    Apr 17, 2018 at 7:51
8

Just use explicit casting:

var stringsize = (strLocalTelefone as NSString).sizeWithAtt...

Otherwise you can bridge it too:
Bridging is no longer supported in later versions of Swift.

var strLocalTelefone = "some string"
var stringsize = strLocalTelefone.bridgeToObjectiveC().sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])

This answer is worth at least looking at, as it highlights potential differences between the two approaches.

4
  • 1
    The bridge is unnecessary. Swift Strings seamlessly bridge to NSString.
    – Bill
    Jun 10, 2014 at 14:56
  • @Bill true, but bridging allows you to call sizeWithAttributes directly, rather than creating a new constant with a name (as shown in the accepted answer). It is less code and basically does the same thing (as bridging gives you an NSString). So I would argue creating a new named constant is unnecessary.
    – Firo
    Jun 10, 2014 at 14:58
  • how can i set button titlelabel text in place of "some string"? Nov 13, 2014 at 7:21
  • You can just access the button's titleLabel: self.myButton.titleLabel.text. Is that what you are looking for
    – Firo
    Nov 13, 2014 at 15:05
7

Just one line solution:

yourLabel.intrinsicContentSize.width for Objective-C / Swift

This will work even your label text have custom text spacing.

2

You can also use this piece of code, it's easier and you don't have to create new variable just to get NSString object:

var stringToCalculateSize:String = "My text"
var stringSize:CGSize = (stringToCalculateSize as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
0

On xCode 6.3, this is what you need to do now:

    let font:AnyObject = UIFont(name: "Helvetica Neue", size: 14.0) as! AnyObject
    let name:NSObject = NSFontAttributeName as NSObject
    let dict = [name:font]
    let textSize: CGSize = text.sizeWithAttributes(dict)
1
  • 1
    Why are you doing all that weird casting? Why not just do let dict = [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 14.0)!] Apr 21, 2015 at 6:39
-3

On xCode 8.0, this is what you need to do now: let charSize = string.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 20)])

1
  • Swift String type doesn't have this function. This is only for NSString. Mar 15, 2017 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.