Skip to content

Instantly share code, notes, and snippets.

@nedimf
Last active November 21, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedimf/99c205f6788cd86c2152b0a6285a32db to your computer and use it in GitHub Desktop.
Save nedimf/99c205f6788cd86c2152b0a6285a32db to your computer and use it in GitHub Desktop.
Truncate string and add "..." in the end - SWIFT
//Extension on String
extension String {
func truncating(max:Int) -> String{
let text = self
print("passed text \(text)")
if text.count >= max {
let truncatedIndex = text.index((text.startIndex), offsetBy: max)
let truncated = text.substring(to: truncatedIndex)
let t = truncated.appending("...")
print("truncated string", t.description)
return t.description
}else{
return self
}
}
}
//Call
label.text = textOfLabel.truncating(max: 24) //max 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment