ios NSAttributedStringKey 给出未解决的标识符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44641877/
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
NSAttributedStringKey giving an unresolved identifier error
提问by Johnm95
I'm following an online tutorial to build a magazine type iOS application. I'm attempting to use NSAttributedStringKey but keep getting the error shown below. Any ideas?
我正在按照在线教程构建杂志类型的 iOS 应用程序。我正在尝试使用 NSAttributedStringKey 但不断收到如下所示的错误。有任何想法吗?
This is the line of code that is causing the error:
这是导致错误的代码行:
let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any]
回答by Tibin Thomas
The projects are probably in different versions of Swift.
这些项目可能使用不同版本的 Swift。
In Swift 4, NSFontAttributeNamehas been replaced with NSAttributedStringKey.font.
在 Swift 4 中,NSFontAttributeName已被替换为NSAttributedStringKey.font。
as stated here NSFontAttributeName vs NSAttributedStringKey.font
如此处所述NSFontAttributeName vs NSAttributedStringKey.font
Need to confirm whether it will work on versions less than ios11
需要确认是否可以在ios11以下的版本上运行
回答by chrismanderson
You are trying to use an iOS 11 API on a pre-iOS 11 version (likely iOS 10). Surprised that you found a tutorial already using beta features!
您正在尝试在 iOS 11 之前的版本(可能是 iOS 10)上使用 iOS 11 API。很惊讶您发现一个教程已经在使用测试版功能!
In the meantime, try this.
与此同时,试试这个。
let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
and that should work.
这应该有效。
回答by rmaddy
The code you are trying to use is a new API added in iOS 11. Since you are using Xcode 8 you are not using iOS 11. So you need to use the current (non-beta) API.
您尝试使用的代码是 iOS 11 中添加的新 API。由于您使用的是 Xcode 8,因此您没有使用 iOS 11。因此您需要使用当前(非测试版)API。
let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
回答by Gautam Sareriya
Swift 4.1 and Xcode 9.3 changes Attributed key value.
Swift 4.1 和 Xcode 9.3 更改了属性键值。
let strName = "Hello Stackoverflow"
let string_to_color2 = "Hello"
let attributedString1 = NSMutableAttributedString(string:strName)
let range2 = (strName as NSString).range(of: string_to_color2)
attributedString1.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: range2)
lblTest.attributedText = attributedString1
Hello will be in red color.
你好将是红色。
回答by Sunil Targe
Swift 4.x
斯威夫特 4.x
// MARK: - Deal with the empty data set
// Add title for empty dataset
func title(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! {
let str = "Welcome"
let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)]
return NSAttributedString(string: str, attributes: attrs)
}
// Add description/subtitle on empty dataset
func description(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! {
let str = "Tap the button below to add your first grokkleglob."
let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)]
return NSAttributedString(string: str, attributes: attrs)
}
// Add your image
func image(forEmptyDataSet _: UIScrollView!) -> UIImage! {
return UIImage(named: "MYIMAGE")
}
// Add your button
func buttonTitle(forEmptyDataSet _: UIScrollView!, for _: UIControlState) -> NSAttributedString! {
let str = "Add Grokkleglob"
let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.callout), NSAttributedStringKey.foregroundColor: UIColor.white]
return NSAttributedString(string: str, attributes: attrs)
}
// Add action for button
func emptyDataSetDidTapButton(_: UIScrollView!) {
let ac = UIAlertController(title: "Button tapped!", message: nil, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Hurray", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
}
回答by Durul Dalkanat
This example works only iOS11.
此示例仅适用于 iOS11。
import UIKit
class VC: UIViewController {
@IBOutlet weak var usernameTxt: UITextField!
@IBOutlet weak var emailTxt: UITextField!
@IBOutlet weak var passTxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}
func setupView() {
usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
passTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
}
}