gpt4 book ai didi

ios - Swift 中的 CGSize sizeWithAttributes

转载 作者:IT王子 更新时间:2023-10-29 04:59:37 26 4
gpt4 key购买 nike

在 objective-C 中我能够使用:

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

但在 Swift 语言中我没有找到任何解决这种情况的方法。

有什么帮助吗?

最佳答案

我做的是这样的:

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

关于ios - Swift 中的 CGSize sizeWithAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24141610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com