 |
 |
Finding and deleting tabs
|
 |
|
 |
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|