ios 类型“NSAttributedStringKey”(又名“NSString”)没有成员“字体”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46520306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:32:49  来源:igfitidea点击:

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

iosswift3cocoapodsnsattributedstringcosmos

提问by Chaudhry Talha

Just updated pods for xcode 9 and I'm getting the error below for Cosmos.

刚刚更新了 xcode 9 的 pod,我收到了Cosmos 的以下错误。

enter image description here

在此处输入图片说明

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'font'

类型“NSAttributedStringKey”(又名“NSString”)没有成员“字体”

回答by Krunal

Note: Ensure swift language version of your project. Here is how you can see/check your swift language version.

注意:确保项目的 swift 语言版本。以下是您如何查看/检查您的 swift 语言版本。

enter image description here

在此处输入图片说明


You have two options as solution to your query:


您有两个选项可以解决您的查询:

  1. If your project has Swift versio 4.0
    - You should choose/download POD compatible to your project's swift language (Share me POD info and swift version, so I can provide you exact pod version version for your pod library suitable for project).

  2. If your project has swift version below 4.0
    - You need to migrate your project into Swift 4.0 (if you've not migrated it). Here is ref question and answer, how to migrate from swift (below) <4.0 to 4.0.

  1. 如果您的项目有 Swift 版本 4.0
    - 您应该选择/下载与您项目的Swift语言兼容的 POD(分享 POD 信息和Swift版本,以便我可以为您的 pod 库提供适合项目的确切 pod 版本)。

  2. 如果您的项目的 swift 版本低于 4.0
    - 您需要将您的项目迁移到 Swift 4.0(如果您还没有迁移它)。这是参考问答,如何从 swift(以下)<4.0 迁移到 4.0。


According to tag added by you in your question - Swift3 is your current project language version and pod 'Cosmos', '~> 12.0' is supporting swift 4.


根据您在问题中添加的标签 - Swift3 是您当前的项目语言版本,而 pod 'Cosmos', '~> 12.0' 支持 swift 4。

pod 'Cosmos', '~> 12.0'

Here is list of previous release supporting Swift version below 4.0.

这是支持低于 4.0 的 Swift 版本的先前版本列表。

https://github.com/evgenyneu/Cosmos/releases

https://github.com/evgenyneu/Cosmos/releases

Try previous release of cosmos like:

尝试以前版本的 Cosmos,例如:

pod 'Cosmos', '~> 11.0.3'
// or
pod 'Cosmos', '~> 11.0.1'
// or
pod 'Cosmos', '~> 11.0.0

'

'

回答by Ace

 attributes: [NSFontAttributeName: label.font]

it's seem okay with swift 3.2 . i fixed with paging menu viewcontroller library.

swift 3.2 似乎没问题。我修复了分页菜单视图控制器库。

回答by Avinash Mishra

In My case I replace This Code

在我的情况下,我替换此代码

[NSAttributedStringKey.font : font]

by Code Below

通过下面的代码

.size(attributes: [NSFontAttributeName : font])

Hope it Helps.

希望能帮助到你。

回答by iPatel

You must need to update swift version to 4.0from Xcode.

您必须需要从 Xcode将swift 版本更新到 4.0

Use below step

使用以下步骤

1) Choose Your Project from project manager (left top from Xcode)
2) Choose Build Settings
3) Search "Swift language"
4) Change "Swift Language Version " => Swift 4.0

1) 从项目管理器中选择您的项目(Xcode 的左上角)
2) 选择构建设置
3) 搜索“Swift 语言”
4) 更改“Swift 语言版本” => Swift 4.0

回答by HafizAnser

let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .subheadline)

回答by Vaibhav Gaikwad

let percentOff = NSMutableAttributedString(string: "\(productOff ?? "0")%OFF", 
attributes: [NSFontAttributeName: UIFont(name: "Montserrat-Regular", 
size: 13)!,NSForegroundColorAttributeName: UIColor.black,
 NSBackgroundColorAttributeName: UIColor.black])
    lblPriceDetail.attributedText = percentOff

This worked for me in swift 4.2

这在 swift 4.2 中对我有用

回答by Leo van der Zee

Your pod might have a wrong swift version, not the project settings.

您的 pod 可能有错误的 swift 版本,而不是项目设置。

To change the pod swift setting, go in the file navigator (left of the screen) to Pods (blue icon) and select the pod you're having problems with. Then go to build settings and go to Swift Language version. Select Swift 4.

要更改 pod swift 设置,请在文件导航器(屏幕左侧)中转到 Pods(蓝色图标)并选择您遇到问题的 pod。然后转到构建设置并转到 Swift 语言版本。选择 Swift 4。