我试图添加到UITextViews的链接,所以我遵循这个职位中的代码。相关的目标-C代码是
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];但当我在斯威夫特2中尝试这个
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: attributedString.string.rangeOfString("/marcelofabri_"))我知道错误了
不能使用类型的参数列表调用“addAttribute”(字符串、值:字符串、范围:范围?)
我需要改变什么才能让它起作用?
发布于 2015-08-26 14:11:07
尝试使用NSString来查找范围而不是Swift String
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: (attributedString.string as NSString).rangeOfString("/marcelofabri_"))发布于 2015-08-26 14:06:55
您使用的是Range,而不是所需的NSRange。看一下:
发布于 2022-03-25 07:05:38
@available(iOS 14, *)
extension ColorPickerVC: UIColorPickerViewControllerDelegate {
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
print(viewController.selectedColor.hexCode)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setHexColor"), object: nil, userInfo: ["hexColor": viewController.selectedColor.hexCode])
}
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setHexColor"), object: nil, userInfo: ["hexColor": viewController.selectedColor.hexCode])
self.view.backgroundColor = viewController.selectedColor
}
func canPerformSegueWithIdentifier(identifier: NSString) -> Bool {
let templates:NSArray = value(forKey: "storyboardSegueTemplates") as! NSArray
let predicate:NSPredicate = NSPredicate(format: "identifier=%@", identifier)
let filteredtemplates = templates.filtered(using: predicate)
return (filteredtemplates.count>0)
}
}https://stackoverflow.com/questions/32228765
复制相似问题