Re: setting tab stops in a document
Re: setting tab stops in a document
- Subject: Re: setting tab stops in a document
- From: Boyd Collier <email@hidden>
- Date: Sat, 5 Jun 2010 16:00:27 -0700
Thanks, Ross and Paul, I've finally got things working the way I wanted. Here's what my code now looks like (in case someone else has a similar problem):
- (void)updateView {
[[self textView] setString:[self string]]; // replaced textView with [self textView] 6-8-06
NSTextView *myTextView = [self textView];
// code to set tab stops, as suggested by PGM
int cnt;
int numStops = 200;
int tabInterval = 80;
NSTextTab *tabStop;
NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init]; //attributes for attributed string of TextView
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setTabStops:[NSArray array]]; // clear all tab stops
// now put in tab stops at desired intervals
for (cnt = 0; cnt < numStops; cnt++) {
tabStop = [[NSTextTab alloc] initWithType:NSLeftTabStopType location:tabInterval*(cnt+1)];
[paraStyle addTabStop:tabStop];
[tabStop release];
}
/** ADDED CODE **/
NSMutableDictionary* typingAttributes = [[myTextView typingAttributes] mutableCopy];
[typingAttributes setObject:paraStyle forKey:NSParagraphStyleAttributeName];
[myTextView setTypingAttributes:typingAttributes];
NSRange rangeOfChange = NSMakeRange(0, [[myTextView string] length]);
[myTextView shouldChangeTextInRange:rangeOfChange replacementString:nil];
[[myTextView textStorage] setAttributes:typingAttributes range:rangeOfChange];
[myTextView didChangeText];
[typingAttributes release];
// end added code
[attrs setObject:paraStyle forKey:NSParagraphStyleAttributeName];
[paraStyle release];
[[textView textStorage] addAttributes:attrs range:NSMakeRange(0, [[[textView textStorage] string] length])];
[attrs release];
}
Boyd
On Jun 5, 2010, at 9:16 AM, Ross Carter wrote:
> On Jun 5, 2010, at 1:09 AM, Boyd Collier wrote:
>
>> In an application I'm working on, I read in plain text files containing data to be analyzed. This uses a slightly-modified version of MyDocument.m that is produced as a result of starting a project with the template for NSDocument architecture. The modification that I made was to add a method named updateView, which is called near the end of windowControllerDidLoadNib and loadDataRepresentation: ofType:
>>
>> My reason for adding this method is to make the spacing of tabs something that works well for the sort of data files being analyzed, which presume tabs are used to separate values being analyzed. Here's the code:
> ...
>> [[textView textStorage] addAttributes:attrs range:NSMakeRange(0, [[[textView textStorage] string] length])];
>
>
> Couple of suggestions:
>
> 1. Call NSTextView -shouldChangeTextInRange:replacementString: before modifying the textStorage, and call -didChangeText afterward.
>
> 2. Look at NSTextView -setDefaultParagraphStyle:.
> _______________________________________________
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden