NSTableCellView padding

How to add add-on to NSTableCellView ?

I want the content to contain 10px, similar to how you did it in plain HTML. (All sides). The text should be vertically centered in the middle.

I am currently doing this in a subclass of NSTextFieldCell. But this does not work correctly.

When I edit text, the edit text field does not use padding from the textfield field.

Image 2: Check image

Check image

Here is the code I have (subclass of NSTextFieldCell )

 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { NSRect titleRect = [self titleRectForBounds:cellFrame]; NSAttributedString *aTitle = [self attributedStringValue]; if ([aTitle length] > 0) { [aTitle drawInRect:titleRect]; } } - (NSRect)titleRectForBounds:(NSRect)bounds { NSRect titleRect = bounds; titleRect.origin.x += 10; titleRect.origin.y = 2; NSAttributedString *title = [self attributedStringValue]; if (title) { titleRect.size = [title size]; } else { titleRect.size = NSZeroSize; } // We don't want the width of the string going outside the cell bounds CGFloat maxX = NSMaxX(bounds); CGFloat maxWidth = maxX - NSMinX(titleRect); if (maxWidth < 0) { maxWidth = 0; } titleRect.size.width = MIN(NSWidth(titleRect), maxWidth); return titleRect; } 
+5
source share
2 answers

Below are some options for filling the gasket.

I used to have problems getting the correct frame height for NSAttributedString . I do not remember how I solved them, but there are some discussions on this issue.

Idea number 1:

Use NSTableView firewall . It is also available in the Builder interface from the table size tab. Find the spacing between cells.

Idea number 2:

When editing the interface:

  • Unless you require something else, use the cell view of the text or image and text table provided by Apple.
  • Change the height of the table cell view in the table view using the size tab.
  • Move the text box inside the table cell view.

Idea number 3:

Use a custom cell.

  • You can change the position of the field editor by overriding -[NSTextFieldCell editWithFrame:inView:editor:delegate:event:] . There also -[NSTextFieldCell setUpFieldEditorAttributes] . I found this sample code useful.
  • If you increase the height of the cell, there are several ways to make NSTextFieldCell draw the text vertically in the center.
+3
source

use the setContentInset method.it sets the distance from the insertion between the content view and the table view of the table.

eg

 self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, -20, 0); 
0
source

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


All Articles