我有以下迅速的4.1代码:
let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time");
timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length));
timeduration.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range: NSRange(location: 0, length: timeduration.length));
timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount));
timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2));
我使用的是Swift 3,并使用Xcode的工具进行了迁移,以实现4.1的快速迁移,但是前景色似乎不起作用。 XCode工具“修复”添加了kCT代码。字体名称和大小似乎可以正常工作。
答案 0 :(得分:0)
修复程序为您提供了错误的密钥。您想改用这样的东西:
timeduration.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: timeduration.length))
所有其他人都一样。
您正在使用的键用于较低级别的核心文本。
答案 1 :(得分:0)
您可以这样做,我希望问题能够得到解决:-
let label = UILabel()
let labelText = "String Text"
let strokeTextAttributes = [
NSAttributedStringKey.strokeColor : UIColor.black,
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.strokeWidth : -2.0,
NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
] as [NSAttributedStringKey : Any]
label.attributedText = NSAttributedString(string: labelText, attributes: strokeTextAttributes)
答案 2 :(得分:0)
这是您的问题的解决方案。在Swift 4.0和更高版本中,有一个名为NSAttributedStringKey的结构定义了字符串属性的键。
let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time")
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length))
timeduration.addAttribute(NSAttributedStringKey.foregroundColor, value:
UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range:
NSRange(location: 0, length: timeduration.length))
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount))
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2))