I need to center a particular word in a sentence by truncating the beginning and endings of a long sentence inside of a UILabel, For example
NSString mySentence = @"This is my very long sentence to give you an example of what I am trying to do.. And its still going..";
NSString myWord = @"example";
<Algorithm goes here>
Should display:
“…sentence to give you an example of what I am trying to…”
If the word is closer to one end or the other, just do your best to center and display an appropriate amount of the text, example:
NSString myWord = @"my";
“This is my very long sentence to give you an example of what…”
Any ideas?
Thanks.
I believe you can do a scan for your search to be placed. Let’s say you have the index in a variable named centerWordIndex. You could then split the string based on whitechars, and add words to the beginning and the end of your word until you are out of words at each side, or until the size of you string matches the size of the label.
what do you think 🙂 ?