2013-03-07 76 views
0

我想調整標準IKImageBrowserCell突出顯示維度的大小。我在stackoverflow here上發現了一個類似的問題(在鏈接中你可以看到標準的IKImageBrowserCell選擇高亮)。更改ImageBrowserCell突出顯示維度

我發現了一些蘋果示例代碼,可以完全定製IKImageBrowserCell以及高亮維度。這些是設置幀

- (NSRect) imageFrame 
{ 
// //get default imageFrame and aspect ratio 
    NSRect imageFrame = [super imageFrame]; 

    if(imageFrame.size.height == 0 || imageFrame.size.width == 0) return NSZeroRect; 

    float aspectRatio = imageFrame.size.width/imageFrame.size.height; 

    // compute the rectangle included in container with a margin of at least 10 pixel at the bottom, 5 pixel at the top and keep a correct aspect ratio 
    NSRect container = [self imageContainerFrame]; 
    container = NSInsetRect(container, 8, 8); 

    if(container.size.height <= 0) return NSZeroRect; 

    float containerAspectRatio = container.size.width/container.size.height; 

    if(containerAspectRatio > aspectRatio){ 
     imageFrame.size.height = container.size.height; 
     imageFrame.origin.y = container.origin.y; 
     imageFrame.size.width = imageFrame.size.height * aspectRatio; 
     imageFrame.origin.x = container.origin.x + (container.size.width - imageFrame.size.width)*0.5; 
    } 
    else{ 
     imageFrame.size.width = container.size.width; 
     imageFrame.origin.x = container.origin.x;  
     imageFrame.size.height = imageFrame.size.width/aspectRatio; 
     imageFrame.origin.y = container.origin.y + container.size.height - imageFrame.size.height; 
    } 

    //round it 
    imageFrame.origin.x = floorf(imageFrame.origin.x); 
    imageFrame.origin.y = floorf(imageFrame.origin.y); 
    imageFrame.size.width = ceilf(imageFrame.size.width); 
    imageFrame.size.height = ceilf(imageFrame.size.height); 

    return imageFrame; 
} 

//--------------------------------------------------------------------------------- 
// imageContainerFrame 
// 
// override the default image container frame 
//--------------------------------------------------------------------------------- 
- (NSRect) imageContainerFrame 
{ 
    NSRect container = [super frame]; 

    //make the image container 15 pixels up 
    container.origin.y += 15; 
    container.size.height -= 15; 

    return container; 
} 

兩個overidden方法和這是的生成高亮選擇

/* selection layer */ 
if(type == IKImageBrowserCellSelectionLayer){ 

    //create a selection layer 
    CALayer *selectionLayer = [CALayer layer]; 

    selectionLayer.frame = CGRectMake(0, 0, frame.size.height, frame.size.height); 

    float fillComponents[4] = {1.0, 0, 0.5, 0.3}; 
    float strokeComponents[4] = {1.0, 0.0, 0.5, 1}; 

    //set a background color 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    color = CGColorCreate(colorSpace, fillComponents); 
    [selectionLayer setBackgroundColor:color]; 
    CFRelease(color); 

    //set a border color 
    color = CGColorCreate(colorSpace, strokeComponents); 
    [selectionLayer setBorderColor:color]; 
    CFRelease(color); 

    [selectionLayer setBorderWidth:2.0]; 
    [selectionLayer setCornerRadius:5]; 

    return selectionLayer; 
} 

這是代碼的結果的代碼:

enter image description here

正如你所看到的那樣,選擇的框架被改變了,但我不能按照我的方式修改它。我只是想要突出顯示爲與圖像縮略圖相同的尺寸。我試圖修改選擇代碼(selectionLayer.frame = CGRectMake(0, 0, frame.size.height, frame.size.height);),但沒有任何反應。任何人都可以幫助我?謝謝!

回答

0

要自行選擇框架佈局,必須覆蓋-[IKImageBrowserCell selectionFrame]


以下是一些您可能要覆蓋的方法,如果你想定製的佈局IKImageBrowserCell的

- (NSRect) imageContainerFrame; 
- (NSRect) imageFrame; 
- (NSRect) selectionFrame; 
- (NSRect) titleFrame; 
- (NSRect) subtitleFrame; 
- (NSImageAlignment) imageAlignment; 

以下是一些您可能要覆蓋的方法,如果你想定製IKImageBrowserCell

- (CGFloat) opacity; 
- (CALayer *) layerForType:(NSString *) type; 
外觀