CocoaDev

Edit AllPages

I have modified the code at FieldEditorIssues to produce a text field which has the appearance of the AddressBook / Library text fields mentioned in HighlightButtonsLikeDLandAB.

I’m completely at a loss as to how to go about turning one of these into a view with many editable text fields. Does anyone have any suggestions as to the best way to approach this? My preliminary thoughts are:

One view with many strings? Or many of these views in a custom superview? As to the first, how to manage the strings and click handling, for the second, how do we deal with multi-line resizing and moving the elements below?

I’ll post any improvements made back here as it seems to be a popular subject at the moment. -BB

Code follows:

@interface FieldEditorTestView : NSView { NSRect itemRect; NSString *itemTitle; NSSize maxSize; NSTextView *mEditor; IBOutlet NSButton *editButton; }

#import “FieldEditorTestView.h”

@implementation FieldEditorTestView

-(void)drawRect:(NSRect)r { [[NSColor whiteColor] set]; NSRectFill(r);

if (mEditor == nil) {
    [[NSColor textBackgroundColor] set];
    NSRectFill(itemRect);
    
    [[NSColor textColor] set];
    NSMutableParagraphStyle *style = [[[NSMutableParagraphStyle alloc] init] autorelease];
    [style setLineBreakMode:NSLineBreakByTruncatingMiddle];
    NSMutableAttributedString *truncatedMiddle = [[NSMutableAttributedString alloc] initWithString:itemTitle];
    [truncatedMiddle addAttribute:NSParagraphStyleAttributeName 
							value:style 
							range:NSMakeRange(0, [itemTitle length])];
							
	if ([itemTitle isEqualToString:NSLocalizedString(@"no value", nil)]) {
		[truncatedMiddle addAttribute:NSForegroundColorAttributeName
								value:[NSColor grayColor]
								range:NSMakeRange(0, [itemTitle length])];
	}
    
    [truncatedMiddle drawInRect:itemRect];
	
} else {
    NSResponder *resp = self window] firstResponder];
    if ([[self window] isKeyWindow] &&
        [resp isKindOfClass:[[[NSView class]] &&
        [(NSView *)resp isDescendantOf:self]) {
       
		[NSGraphicsContext saveGraphicsState];
       
		
			[self setFocusRingType:NSFocusRingTypeNone];
			
			NSRect outerRect = [mEditor frame];
			outerRect.size.width += 2;
			outerRect.size.height += 4;
			outerRect.origin.x -= 1;
			outerRect.origin.y -= 2;
			
			// Shadow for line
			NSShadow *shadow = [[NSShadow alloc] init];
			[shadow setShadowOffset:NSMakeSize(0, -2)];
			[shadow setShadowBlurRadius:4.0];
			[shadow setShadowColor:[NSColor colorWithDeviceWhite:0.5 alpha:0.3]];
			[shadow set];
			
			[[NSColor colorWithCalibratedRed:0.2226 green:0.46875 blue:0.83593 alpha:1.0] set];
			NSFrameRect(outerRect);

			// A sort of nil-shadow to avoid restoring the graphics state already
			[shadow setShadowOffset:NSZeroSize];
			[shadow setShadowBlurRadius:0];
			[shadow setShadowColor:[NSColor clearColor]];
			[shadow set];
			
			// Shrink outerRect to avoid drawing over line
			outerRect.size.width -= 2;
			outerRect.size.height -= 2;
			outerRect.origin.x += 1;
			outerRect.origin.y += 1;
			[[NSColor whiteColor] set];
			NSRectFill(outerRect);	
			
			[shadow release];
			shadow = nil;
        [NSGraphicsContext restoreGraphicsState];
    }
} }

@end