0

IKImageBrowserCell標準のハイライト ディメンションのサイズを変更したい。ここでstackoverflowで同様の質問を見つけました(リンクで、標準のIKImageBrowserCell選択のハイライトを見ることができます)。

ハイライト ディメンションとハイライト ディメンションを完全にカスタマイズするアップル サンプル コードをいくつか見つけましたIKImageBrowserCell。これらは、フレームを設定する 2 つのオーバーライドされたメソッドです。

- (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 に答える