簡體   English   中英

用可可繪制線分隔符

[英]Drawing Line Separator with Cocoa

如何繪制一個行分隔符,如下所示:

在此輸入圖像描述

請注意,彼此頂部有2條單像素線。 有小費嗎?


編輯:

這是我需要的代碼,在NSBox子類中:(或NSView,並不重要):

- (void)drawRect:(NSRect)rect
{
    [[NSColor lightGrayColor] set];
    NSRectFill(NSMakeRect(0, 1, NSWidth(rect), 1));

    [[NSColor whiteColor] set];
    NSRectFill(NSMakeRect(0, 0, NSWidth(rect), 1));    
}

通常,這種分隔符是使用NSBox繪制的,通常配置為NSBoxSeparator類型。 但這並不是你想要的外觀。 我建議在NSBox子類中NSBox它(這樣你就可以獲得正常的NSBox行為)。 有關NSBox子類的示例,請參閱Matt Gemmell的RoundedBox

你只需要兩行,所以drawRect:應該非常簡單。 以這種方式封裝它將使您非常靈活。

(當然,您也可以考慮使用標准的NSBox分隔符,而不是創建自定義外觀。這就是它的用途。)

我使用iOS模仿它,假設NSView有類似的界面,我希望這會有所幫助:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(11, 99, 298, 1)];
lineView.backgroundColor = [UIColor darkGrayColor];
[panel addSubview:lineView];

UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 1)];
lineView2.backgroundColor = [UIColor whiteColor];
[panel addSubview:lineView2];

看看我自己的NSView界面,它似乎幾乎可以直接轉移:

NSView *lineView = [[NSView alloc] initWithFrame:CGRectMake(11, 99, 298, 1)];
lineView.backgroundColor = [NSColor colorWithCalibratedRed:0.4f green:0.4f blue:0.4f alpha:1.0f];
[panel addSubview:lineView];

NSView *lineView2 = [[NSView alloc] initWithFrame:CGRectMake(10, 100, 300, 1)];
lineView2.backgroundColor = [NSColor colorWithCalibratedRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
[panel addSubview:lineView2];

你也可以使用CALayer

CALayer *lineLayer = [CALayer layer];
lineLayer.frame = (CGRectMake(0, 100, 300, 1));
[lineLayer setBackgroundColor:CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0)];
[panel.layer addSublayer: lineLayer];

或(如果您無權訪問圖層)

CALayer *lineLayer = [CALayer layer];
lineLayer.frame = (CGRectMake(0, 100, 300, 1));
[lineLayer setBackgroundColor:CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0)];
[panel setWantsLayer:YES];
[panel setLayer: lineLayer];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM