0

我想调整标准高IKImageBrowserCell光尺寸。我在这里发现了关于 stackoverflow 的类似问题(在链接中您可以看到标准IKImageBrowserCell选择突出显示)。

我发现了一些完全自定义的苹果示例代码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;
}

这是生成选择突出显示的代码

/* 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;
}

这是该代码的结果:

在此处输入图像描述

如您所见,选择的框架已更改,但我无法以我不希望的方式对其进行修改。我只是想让高光与图像缩略图的大小相同。我试图修改选择代码 ( selectionLayer.frame = CGRectMake(0, 0, frame.size.height, frame.size.height);) 但没有任何反应。任何人都可以帮助我吗?谢谢!

4

1 回答 1

0

要自定义您的选择框布局,您必须覆盖-[IKImageBrowserCell selectionFrame]


如果您想要自定义IKImageBrowserCell的布局,以下是您可能想要覆盖的方法。

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

如果您想自定义IKImageBrowserCell的外观,以下是您可能想要覆盖的方法

- (CGFloat) opacity;
- (CALayer *) layerForType:(NSString *) type;
于 2013-05-05T18:57:43.943 回答