IKImageBrowserView可以显示多行标题吗? [英] Can IKImageBrowserView display multi-line titles?

查看:239
本文介绍了IKImageBrowserView可以显示多行标题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 IKImageBrowserView 显示长标题,包装他们并显示它们在多行,但我已经无法实现这一点。



我试着改变 ImageBrowserCell 子类中返回的titleFrame的大小,并且还设置标题上的段落样式,以便它应该换行( NSLineBreakByWordWrapping ),但我只得到一行文本。



有没有人试过?我可能会尝试其他任何建议吗?

解决方案

恐怕不容易。一种方法是将 imageTitle 留空并在 CALayer 上绘制文本。我能够使它工作得相当不错的结果:





第一件事 - 因为你不会使用标准 imageTitle 属性,您希望在< IKImageBrowserItem> - 成功数据对象中创建新的标题属性。然后,您要创建一个 IKImageBrowserCell

code>子类。请务必告诉您 IKImageBrowserView 您使用的是自订储存格;我只是为此写了一个快速类别:

  @implementation IKImageBrowserView(CustomCell)
- (IKImageBrowserCell *)newCellForRepresentedItem: (id)cell
{
return [[MyCustomImageBrowserCell alloc] init];
}
@end

接下来,在您的自定义单元格中,覆盖方法 - (CALayer *)layerForType:(NSString *)type 。为特定图层位置类型创建并返回CALayer:

   - (CALayer *)layerForType:(NSString *)type 
{
if([type isEqualToString:IKImageBrowserCellBackgroundLayer])
{
CALayer * layer = [CALayer layer];
layer.delegate = self;
layer.frame = self.frame; //单元格的框架
layer.name = type; //如果需要,在绘制时引用图层位置类型的方式
[layer setNeedsDisplay];
return layer;
}

return [super layerForType:type];
}

然后,实现 -drawLayer:inContext: / code>方法来处理图层的绘图:

   - (void)drawLayer: )layer inContext:(CGContextRef)context 
{
//设置当前上下文。
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext * nscg = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
[NSGraphicsContext setCurrentContext:nscg];

NSString * title = [self.representedItem title];

//换行和居中文本
NSMutableParagraphStyle * paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
[paragraphStyle setAlignment:NSCenterTextAlignment];

NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
[attrs setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[attrs setObject:[NSFont systemFontOfSize:12.0f] forKey:NSFontAttributeName];

CGFloat padding = 4.0f;

[title drawInRect:NSMakeRect(
0,
- (self.imageFrame.size.height + padding),
self.frame.size.width,
100
)withAttributes:attrs];

[NSGraphicsContext restoreGraphicsState];不幸的是,这种方法确实有一些缺点。这种方法有很多不足之处。文本没有像 imageTitle 这样的蓝色选择背景。然而,您可以使用 IKImageBrowserCellSelectionLayer 的自定义CALayer内的 NSBezierPath 来绘制您自己的选择背景。 >

希望这已经够用了。


I'd like my IKImageBrowserView to display long titles by wrapping them and displaying them in multiple lines, but I've been unable to achieve this.

I've tried changing the size of the titleFrame returned in the ImageBrowserCell subclass and also setting the paragraph style on the title so that it should wrap (NSLineBreakByWordWrapping) but I only ever get a single line of text.

Has anyone tried this? Any other suggestion I might try?

解决方案

Not easily, I'm afraid. One way you can do it is by leaving imageTitle blank and drawing text on a CALayer. I was able to make it work with fairly decent results:

First things first -- since you're not going to be using the standard imageTitle property, you want to create a new title property in your <IKImageBrowserItem>-conforming data object. I called mine simply title.

Then, you want to create an IKImageBrowserCell subclass. Make sure to tell your IKImageBrowserView you're using a custom cell; I just wrote a quick category for this:

@implementation IKImageBrowserView(CustomCell)
- (IKImageBrowserCell *)newCellForRepresentedItem:(id)cell
{
    return [[MyCustomImageBrowserCell alloc] init];
}
@end

Next, in your custom cell, override the method - (CALayer *)layerForType:(NSString *)type. Create and return a CALayer for a specific layer location type:

- (CALayer *)layerForType:(NSString *)type
{    
    if( [type isEqualToString:IKImageBrowserCellBackgroundLayer] )
    {       
        CALayer *layer = [CALayer layer];
        layer.delegate = self;
        layer.frame = self.frame; // the cell's frame
        layer.name = type; // a way to refer to the layer location type during drawing if need be
        [layer setNeedsDisplay];
        return layer;
    }

    return [super layerForType:type];
}

Then, implement the -drawLayer:inContext: method in your custom cell to handle the layer's drawing:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    // Set the current context.
    [NSGraphicsContext saveGraphicsState];
    NSGraphicsContext *nscg = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
    [NSGraphicsContext setCurrentContext:nscg];

    NSString *title = [self.representedItem title];

    // Wrap and center the text
    NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
    [paragraphStyle setAlignment:NSCenterTextAlignment];

    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    [attrs setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    [attrs setObject:[NSFont systemFontOfSize:12.0f] forKey:NSFontAttributeName];

    CGFloat padding = 4.0f;

    [title drawInRect:NSMakeRect(
        0, 
        -(self.imageFrame.size.height + padding), 
        self.frame.size.width, 
        100
    ) withAttributes:attrs];

    [NSGraphicsContext restoreGraphicsState];
}

Unfortunately, this approach does have a few drawbacks. The text doesn't get a blue selection background like imageTitle does. You can, however, draw your own selection background using an NSBezierPath inside a custom CALayer for the IKImageBrowserCellSelectionLayer.

Hope this is enough to go on.

这篇关于IKImageBrowserView可以显示多行标题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆