Last active
November 21, 2020 20:22
-
-
Save nedimf/99c205f6788cd86c2152b0a6285a32db to your computer and use it in GitHub Desktop.
Truncate string and add "..." in the end - SWIFT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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