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?

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
Naldo Lopes
  • 1,191
  • 2
  • 11
  • 13

6 Answers6

160

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)])
Community
  • 1
  • 1
holex
  • 23,961
  • 7
  • 62
  • 76
  • @holex What if I am using Custom font? – Developer Nov 12 '14 at 10:49
  • @Developer, obviously you need to use the custom font instead of the system font. – holex Nov 12 '14 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 '14 at 10:54
  • @Developer, the `UIFont` class reference can give you more information how you can init a custom font, I would say to take a closer look on the `+fontWithName:size:` method. (here is the complete reference: https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIFont_Class/) – holex Nov 12 '14 at 10:58
  • @holex Yes I saw that already but I am not able to call this function in the following line.---- let size: CGSize = myString.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])----. How Helvetica with size 15 can be set in the attribute? – Developer Nov 12 '14 at 11:07
  • @holex Any opinion on this? – Developer Nov 12 '14 at 11:28
  • 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 '14 at 11:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64783/discussion-between-developer-and-holex). – Developer Nov 12 '14 at 11:45
  • How can i get button text? – Toseef Khilji Nov 13 '14 at 07:21
  • @Virussmca, have you taken a look on the `UIButton` class reference? you would have found a method, called `–titleForState:`, that gives the text of your instance. (_source_: https://developer.apple.com/library/ios/documentation/uikit/reference/uibutton_class/index.html) – holex Nov 13 '14 at 09:05
  • @holex: yes i can able to get button text but it gives errors, can you just edit your answer for getting text size from button ? – Toseef Khilji Nov 13 '14 at 11:13
  • @Virussmca, unfortunately I can't update my answer with that, the OP has no such request of any buttons or its any sizes. If you have a new issue, please raise a new post about it and the community will probably answer to you. – holex Nov 13 '14 at 11:56
  • I have a unicode string so how can i get the size of that string using sizeWithAttributes? – Navneet Sharma Apr 16 '18 at 15:09
  • 1
    @NavneetSharma, `String` in Swift fully supports unicode characters, so it should be no difference at all. – holex Apr 17 '18 at 07:51
  • no need for the NSAttributedStringKey.font it can be written as .font it is inferred to be NSAttributedStringKey ==> size(withAttributes: [.font : UIFont(...)]) – J. V. Sep 10 '18 at 04:37
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.

Community
  • 1
  • 1
Firo
  • 15,448
  • 3
  • 54
  • 74
  • 1
    The bridge is unnecessary. Swift `String`s seamlessly bridge to `NSString`. – Bill Jun 10 '14 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 '14 at 14:58
  • how can i set button titlelabel text in place of "some string"? – Toseef Khilji Nov 13 '14 at 07:21
  • You can just access the button's `titleLabel`: `self.myButton.titleLabel.text`. Is that what you are looking for – Firo Nov 13 '14 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.

Alok
  • 24,880
  • 6
  • 40
  • 67
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)])
Baki
  • 490
  • 4
  • 19
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)
user2962499
  • 486
  • 6
  • 10
  • 1
    Why are you doing all that weird casting? Why not just do `let dict = [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 14.0)!]` – Aaron Brager Apr 21 '15 at 06:39
-3

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