How to use NSArrayController + NSTableView

http://www.cocoadev.com/index.pl?NSArrayController

Setting heading levels


if (![[range startContainer] isEqualNode:[range endContainer]])

return NO; // does not work with cross-node selections

if ([range startOffset] == [range endOffset])

return NO; // no selection.

// remove any heading tags from parents.

NSString *textContent = [[range startContainer] textContent];

NSString *leftStr = nil;

NSString *midStr = nil;

NSString *rightStr = nil;

// there is always a mid part, that's selected

midStr = [textContent substringWithRange:NSMakeRange([range startOffset], [range endOffset]-[range startOffset])];

// is any text left on the left

if ([range startOffset] > 0)

leftStr = [textContent substringToIndex:[range startOffset]];

// is any text left on the right

if ([range endOffset] < [[[range startContainer] textContent] length])

rightStr = [textContent substringFromIndex:[range endOffset]];

DOMNode *mid = [[range startContainer] parentElement]; // parent elements contains the tags wrapping the DOMText

[mid setTextContent:midStr];

if (leftStr != nil) {

DOMNode *first = [mid cloneNode:YES];

[first setTextContent:leftStr];

[[mid parentNode] insertBefore:first refChild:mid];

}

if (rightStr != nil) {

DOMNode *last = [mid cloneNode:YES];

[last setTextContent:rightStr];

[[mid parentNode] appendChild:last];

}

if (level > 0) {

[self wrapNewTag:[NSString stringWithFormat:@"h%d", level] aroundContentsOfElementNode:(DOMElement *)mid];

} else {

[self wrapNewTag:[NSString stringWithFormat:@"p", level] aroundContentsOfElementNode:(DOMElement *)mid];

}

[self HTMLUpdated];

return YES;

Copy Paste in WebView: Links

Notes:

Pasteboard programming guide:

source: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PasteboardGuide106/Introduction/Introduction.html

User Interface Validation example for enabling/disabling copy/paste buttons:

source: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/UIValidation/Articles/implementingValidation.html

WebKit pasteboard:

source: https://developer.apple.com/library/mac/#documentation/appleapplications/conceptual/safarijsprogtopics/tasks/CopyAndPaste.html

WebKit dragging:
notes: by default for images in editable mode. can be added to custom divs!
source: https://developer.apple.com/library/mac/#documentation/appleapplications/conceptual/safarijsprogtopics/tasks/DragAndDrop.html

DOMTreeWalker


DOMTreeWalker *treeWalker = [webView.mainFrameDocument createTreeWalker:[range commonAncestorContainer] whatToShow:DOM_SHOW_ALL filter:nil expandEntityReferences:YES];

while ([treeWalker nextNode]) {

NSLog(@"string rep:%@\n text:%@", [[treeWalker currentNode] stringRepresentation], [[treeWalker currentNode] textContent]);

}

Buttons to Imitate Format Menu Items in NSDocument

A discussion on why you can not use a button to imitate the format menu bar item for toggling bold and italic attributes:

http://www.cocoabuilder.com/archive/cocoa/165244-changing-fonts-without-font-menu.html#165349

in a nutshell, you use the tag to distinguish between bold (2) and italic (1) traits, but the toggle direction is determined by calling the validateMenuItem method which requires the sender to be a menuItem?

Detecting user inactivity/idle time since last screen touch

Found this gem on stackoverflow. Very useful for the lazy annotation loading on maps when combined with mapview:regionWillChangeAnimated and mapview:regionDidChangeAnimated delegates.

Objective C Programming Tips

Very useful and easy to follow tips:

“5) Set your properties as nonatomic. They’re atomic by default and upon synthesis, semaphore code will be created to prevent multi-threading problems. 99% of you probably don’t need to worry about this and the code is much less bloated and memory efficient when set to nonatomic.”

source

Covering all Annotations on MKMapView

How to set the region for MKMapView in order to cover all annotations nicely:


-(void)zoomToFitMapAnnotations:(MKMapView*)mapView
{
if([mapView.annotations count] == 0)
return;

CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(MapAnnotation* annotation in mapView.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fabs.html">fabs</a>(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fabs.html">fabs</a>(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}

from codis.

Good Memory Management Practice in Objective C

  1. Always use properties for everything to be consistent, even non-public class scoped variables should be done as properties unless there is a good reason not to (this will help us later if we run into threading issues and have to do any locking).
  2. Always set and get via properties calls (I.e. self.property, not bare assignment or accessing ivars).
  3. EXCEPT in the init and dealloc methods. Apple recommends directly accessing ivars in these two cases to avoid side-effects. That may mean manual retains in the init methods in some cases (as opposed to property based e.g. nonatomic, retain declaration).
S. Petschulat

Google’s Search Engine Optimization Starter Guide

http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/en//webmasters/docs/search-engine-optimization-starter-guide.pdf

Follow

Get every new post delivered to your Inbox.