Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Finding and deleting tabs

Finding and deleting tabs
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 15, 2004, 12:00 PM
 
Hey,

I am making a text editor and would like to add a feature similar to SubEthaEdit. This feature is the Shift Left and Shift Right feature.

Basically, I can shift right easily (it seems to work perfectly) here is my code:

Code:
- (void)shiftRight:(id)sender; { NSRange where = [docTextView selectedRange]; NSString *selectedString = [[[docTextView textStorage] string] substringWithRange:where]; NSArray *lines = [selectedString componentsSeparatedByString:@"\n"]; if (![selectedString isEqualToString:@""]) { NSMutableString *newString = [[NSMutableString alloc] init]; int i; for (i=0;i<[lines count];i++) { NSString *lineString = [NSString stringWithFormat:@"\t%@", [lines objectAtIndex:i]]; [newString appendString:lineString]; } [docTextView replaceCharactersInRange:where withString:newString]; [docTextView setSelectedRange:NSMakeRange(where.location, [newString length])]; } }
But I am having trouble figuring out how I can shift text left, a tab at a time. So basically, this is how it should work, the user highlights some text, clicks the shift left item, the code looks for a tab in the code, and if found deletes one (and only one). The problem I am having, is isolating one tab incase there are many tabs, and deleting one at a time.

Any ideas appreciated,
Oliver
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jul 15, 2004, 12:20 PM
 
You could try checking to see whether the first character in a line is a tab, and then delete the first character if it is a tab.

Maybe?
[Wevah setPostCount:[Wevah postCount] + 1];
     
Professional Poster
Join Date: Sep 1999
Location: Ottawa, ON, Canada
Status: Offline
Reply With Quote
Jul 15, 2004, 01:49 PM
 
I agree with Wevah. Do it line by line basis, not character by character basis. Check char, if tab, delete - then search for next line , not character, and repeat.
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 15, 2004, 02:00 PM
 
Right,

I got it to semi-work, but I am having a little problem:

Code:
- (void)shiftLeft:(id)sender; { NSRange where = [docTextView selectedRange]; NSString *selectedString = [[[docTextView textStorage] string] substringWithRange:where]; NSArray *lines = [selectedString componentsSeparatedByString:@"\n"]; NSMutableString *newString = [[NSMutableString alloc] init]; if (![selectedString isEqualToString:@""]) { int i; for (i=0;i<[lines count];i++) { NSString *lineString = [NSString stringWithFormat:@"%@", [lines objectAtIndex:i]]; if ([lineString length] > 0 && [lineString characterAtIndex:0] == '\t') { lineString = [lineString substringFromIndex:1]; } [newString appendString:lineString]; if (i!=[lines count]-1) [newString appendString:@"\n"]; } [docTextView replaceCharactersInRange:where withString:newString]; [docTextView setSelectedRange:NSMakeRange(where.location, [newString length])]; } }
So if for example I have some code that looks like this:

*Tab*Hello

It moves Hello right to the left.

But I am having a problem with some webpages that have code like:

*Space**Tab**Tab*Hello

This basically means my code won't function at all correctly. Since a space is the first char. Anyone got any ideas?

Thanks,
Oliver
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Jul 16, 2004, 02:38 AM
 
You could use [NSCharacterSet whitespaceCharacterSet] with NSString's rangeOfCharacterFromSet:

Or, you could possibly do:

if ([lineString length] > 0 && ([lineString characterAtIndex:0] == '\t' || [lineString characerAtIndex:0] == ' '))
[Wevah setPostCount:[Wevah postCount] + 1];
     
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
Jul 16, 2004, 02:42 AM
 
Originally posted by Wevah:
You could use [NSCharacterSet whitespaceCharacterSet] with NSString's rangeOfCharacterFromSet:

Or, you could possibly do:

if ([lineString length] > 0 && ([lineString characterAtIndex:0] == '\t' || [lineString characerAtIndex:0] == ' '))
I ended up using your bottom method, thanks.

Oliver
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2