Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 3, 2012, 08:33 PM   #1
newtoiphonesdk
macrumors 6502a
 
Join Date: Jul 2010
Replace parts of text strings

In my app I have a plist of many quotes. These quotes are loaded into a UILabel, and has the option to share with Twitter. Some quotes are quite long, and I know I could easily cut all characters past the first 140, but I would like to include the name attributed to the quote at the end. Each quote is setup like this:
"I just said something famous." - Me
If the UILabel.text length is over 140 I would like to get the length, and subtract 140 from it, so I know how many characters must be removed. I have this part all setup in my code already. What I'm not for sure how to do is this:
If the quote has 200 characters, find " -
Then, remove the 63 characters immediately in front of " -
Replace those 63 characters with ...
This way a quote that looks like what I put above, if too long, would end up looking like:
"I just said someth..." - Me

I realize that there will be many occasions that it cuts a word off right in the middle, but I am willing to live with that. Any suggestions for removing a certain number of characters before the " - in the quote?
newtoiphonesdk is offline   0 Reply With Quote
Old Aug 4, 2012, 01:30 AM   #2
NickFalk
macrumors 6502
 
Join Date: Jun 2004
If you are certain that the symbol - does not show up elsewhere in any of the quotes you could simply do this in a two step process.

First separate the original string by using the componentsSeparatedByString: method. You should then have an array with two strings. The first one consisting of the actual quote and the second of the name. Then you can use the part of your quote that shortens the quote on the first string and output them however you like.

Personally I would rework your method for shortening the quote as well. Rather than simply counting the letters you can separate the string by white space @" " thus ending up with an array of all the single words. Then use a mutable string and while loop structure to add each single word (+ a white space) from the array of words. Simply run the loop until the length of the mutable string + the next word in the array are larger than the 140 characters.

Last edited by NickFalk; Aug 4, 2012 at 03:16 AM. Reason: typo
NickFalk is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:41 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC