ios - NSFontAttributeName 与 NSAttributedStringKey.font

标签 ios swift3

我在使用库中的 Swift 代码时遇到了一些问题,我已经使用了一段时间。 这似乎与某种版本冲突有关,但我不确定。

代码如下:

let attribMsg = NSAttributedString(string: msg,
                               attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 23.0)])

这是我从编译器得到的错误信息:

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
    Did you mean 'zone'?  Fix(button)

enter image description here

在不同的项目中使用这段代码,我注意到在其中一些项目上我没有收到错误消息,并且所有编译都没有任何问题。

我还注意到,如果我用以下代码替换上面的代码:

let attribMsg = NSAttributedString(string: msg,
                               attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 23.0)])

有问题的项目将工作,而其他项目(以前工作)显示其他消息:

'NSFontAttributeName' has been renamed to 'NSAttributedStringKey.font'

enter image description here

换句话说,一些项目使用一种类型的代码,而另一些项目使用另一种类型的代码。

不用说,我不想每次从一个项目切换到另一个项目时都更改代码。

我所做的更改项目部署目标的实验似乎没有太大区别。 那么我的问题来了:处理这个问题的方法是什么?

最佳答案

这些项目可能使用不同版本的 Swift。

在 Swift 4 中,NSFontAttributeName 已被替换为 NSAttributedStringKey.font

在 Swift 5 中,NSFontAttributeName 已被替换为 NSAttributedString.Key.font

关于ios - NSFontAttributeName 与 NSAttributedStringKey.font,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966681/

相关文章:

android - firebase API 中的 MismatchSenderID 错误

iphone - MPMoviePlayerController : Player hides Player Controls permenantly only in blow iOS6

ios - 在 iOS 10 中使异步调用有效同步的最佳方法

arrays - 字符串快速追加到数组

ios - Swift 3 - 以编程方式构建带有图像和标签的按钮

php - 将大于 1MB/iOS 的文件上传到服务器

ios - 如何将 UIView 转换为图像

ios - 强制 UINavigationBar 覆盖下面的容器而不是将其向下推

Swift函数在另一个函数完成后执行

swift - 知道保留循环在哪里并删除它们