CGSize sizeWithAttributes in Swift

IosSwiftCgsize

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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.

Solution 3 - Ios

Just one line solution:

yourLabel.intrinsicContentSize.width for Objective-C / Swift

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

Solution 4 - Ios

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

Solution 5 - Ios

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)

Solution 6 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaldo LopesView Question on Stackoverflow
Solution 1 - IosholexView Answer on Stackoverflow
Solution 2 - IosFiroView Answer on Stackoverflow
Solution 3 - IosAlokView Answer on Stackoverflow
Solution 4 - IosBakiView Answer on Stackoverflow
Solution 5 - Iosuser2962499View Answer on Stackoverflow
Solution 6 - Ioszhang douView Answer on Stackoverflow