Can IKImageBrowserView display multi-line headers?

I would like my IKImageBrowserView display long headers, wrapping them and displaying them in several lines, but I could not achieve this.

I tried resizing the titleFrame returned in a subclass of ImageBrowserCell , and also set the paragraph style in the title so that it turned around ( NSLineBreakByWordWrapping ), but I only get one line of text.

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

+4
source share
1 answer

Not easy, I'm afraid. One way to do this is to leave imageTitle blank and the picture text on CALayer . I was able to get it to work with pretty decent results:

enter image description here

First of all, since you are not going to use the standard imageTitle property, you want to create a new title property in the <IKImageBrowserItem> -conforming data object. I just called me title .

Then you want to subclass IKImageBrowserCell . Be sure to tell your IKImageBrowserView that you are using a custom cell; I just wrote the following category for this:

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

Then in your user cell redefine the method - (CALayer *)layerForType:(NSString *)type . Create and return a CALayer for a specific type of layer location:

 - (CALayer *)layerForType:(NSString *)type { if( [type isEqualToString:IKImageBrowserCellBackgroundLayer] ) { CALayer *layer = [CALayer layer]; layer.delegate = self; layer.frame = self.frame; // the cell 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 we implement the -drawLayer:inContext: method in your custom cell to process the layer 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 has several drawbacks. The text does not get a blue selection background, for example imageTitle . However, you can make your own selection background using NSBezierPath inside a custom CALayer for IKImageBrowserCellSelectionLayer .

I hope this is enough to continue.

+6
source

Source: https://habr.com/ru/post/1338171/


All Articles