سؤال

هذا هو الكود الخاص بي:

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
    // Create the Gradient 
    NSGradient *fillGradient = nil;
    if (mouseIsDown)
        fillGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.868 green:0.873 blue:0.868 alpha:1.000]  endingColor:[NSColor colorWithCalibratedRed:0.687 green:0.687 blue:0.687 alpha:1.000]];
    else
        fillGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.687 green:0.687 blue:0.687 alpha:1.000] endingColor:[NSColor colorWithCalibratedRed:0.868 green:0.873 blue:0.868 alpha:1.000]];
    // Add The Text
    NSDictionary *att = nil;

    NSMutableParagraphStyle *style =
    [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [style setLineBreakMode:NSLineBreakByWordWrapping];
    [style setAlignment:NSLeftTextAlignment];
    att = [[NSDictionary alloc] initWithObjectsAndKeys:
           style, NSParagraphStyleAttributeName, 
           [NSColor blackColor],
           NSForegroundColorAttributeName, nil];
    [style release];

    // Create the path
    aPath = [[NSBezierPath bezierPath] retain]; 

    [aPath moveToPoint:NSMakePoint(10.0, 0.0)];
    [aPath lineToPoint:NSMakePoint(70.0, 0.0)];
    [aPath lineToPoint:NSMakePoint(70.0, 23.0)];
    [aPath lineToPoint:NSMakePoint(10.0, 23.0)];
    [aPath lineToPoint:NSMakePoint(0.0, 10.0)];

    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowColor:[NSColor blackColor]];
    [shadow setShadowOffset:NSMakeSize(0, 0)];
    [shadow setShadowBlurRadius:5];
    [shadow set];

    NSRect rect;
    rect.size = [[self title] sizeWithAttributes:att];
    rect.origin.x = floor( NSMidX([self bounds]) - rect.size.width / 2 - 8);
    rect.origin.y = floor( NSMidY([self bounds]) - rect.size.height / 2 - 5);

    [fillGradient drawInBezierPath:aPath angle:90.0];
    [fillGradient release];
    [[self title] drawInRect:rect withAttributes:att];
    [att release];

} المشكلة هي أن nsshadow تقف وراء النص وليس nsbezierpath ، 'Apath' ، كيف يمكنني إضافة الظل the nsbezierpath؟

هل كانت مفيدة؟

المحلول

حاول لف أشياء ملء مسار NSSHADOW و BEZIER (على سبيل المثال ، من aPath = [[NSBezierPath bezierPath] retain]; إلى [fillGradient release];) في [NSGraphicsContext saveGraphicsState] و [NSGraphicsContext restoreGraphicsState].

أيضًا ، قد يتم قص الظل حسب حدود العرض (لا أتذكر ما إذا كان هذا صحيحًا أم لا).

(بالإضافة إلى ذلك ، فإن الطريقة "الأكثر ملاءمة" للقيام بذلك ربما تكون للفئة الفرعية NSButtonCell كذلك ، تجاوز drawWithFrame:inView:/-drawInteriorWithFrame:inView: وإعادة فئة الخلية المخصصة من الزر الخاص بك +cellClass الطريقة (التأكد من تعيين فئة الخلايا المناسبة في IB أيضًا ، أو تبديلها في الزر الخاص بك -initWithCoder:). قد تلبي الطريقة التي تفعل بها ذلك احتياجاتك بشكل جيد.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top