I need to get the xy coordinate of a character in a String, but couldn't find any up to date answers. How can I achieve this? I found this article here: Swift : How do I find the position(x,y) of a letter in a UILabel? but .rangeOfString is not available anymore:
extension String {
func characterPosition(character: Character, withFont: UIFont = UIFont.systemFontOfSize(18.0)) -> CGPoint? {
guard let range = self.rangeOfString(String(character)) else {
print("\(character) is missed")
return nil
}
let prefix = self.substringToIndex(range.startIndex) as NSString
let size = prefix.sizeWithAttributes([NSFontAttributeName: withFont])
return CGPointMake(size.width, 0)
}
Do you know how to get it to work again?