SwiftUI - String function size(withAttributes:) throwing NSInvalidArgumentException

590 Views Asked by At

The following code is throwing this exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__SwiftValue renderingMode]: unrecognized selector sent to instance 0x6000007122b0'

let helloWorld = "Hello World"
let width = helloWorld.size(withAttributes: [.font: Font.system(size: 16.0)])
print("\(width)")

Has anyone figured out why this isn't working or a potential workaround? This is happening in Swift 5 targeting iOS 13.2

1

There are 1 best solutions below

2
E.Coms On

The structure Font is different from the class UIFont. The following works.

    let width = helloWorld.size(withAttributes: [.font: UIFont.systemFont(ofSize: 16.0)])