I'm storing font preferences for a WebView in my shared user defaults. I have a WebView set up in my XIB, but it isn't possible to bind a WebView's font properties in IB, so I'm wondering if I can do it in code instead. I've tried this:
WebPreferences *webPrefs = [WebPreferences standardPreferences];
[webPrefs bind:#"fixedFontFamily"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:#"values.webViewMonospaceFontFamily"
options:nil];
[myWebView setPreferences:webPrefs];
and it doesn't work.
If this is not going to work, what would be a working strategy for changing my WebView's font when the user selects a new font in my preferences window? Do I have to manually set it in my changeFont: method?
Update. This does work. WebPreferences has more font properties than I thought, and fixedFontFamily was the wrong one for the page I was viewing.
Related
As of OSX 10.7 if you wish to perform a manual layout of a NSView you should do so by overriding the layout method and whenever you wish to schedule a call to this method you simply do:
[myView setNeedsLayout:YES]
I am very familiar with this pattern in iOS however on OSX it does not seem to work. I have created a custom NSView and implemented layout but it seems that it never gets called. Ever. Not after adding subviews, not after calling setNeedsLayout:YES, not ever and I don't know why. I can manually call layout and things work as expected but the docs say never to do this.
From Xcode:
- (void)layout
Override this method if your custom view needs to perform custom
layout not expressible using the constraint-based layout system. In
this case you are responsible for calling setNeedsLayout: when
something that impacts your custom layout changes.
From the online docs:
You should only override layout only if you want to do custom layout.
If you do, then you also need to invoke setNeedsLayout: when something
that feeds into your custom layout changes.
Link: http://developer.apple.com/library/mac/#releasenotes/UserExperience/RNAutomaticLayout/#//apple_ref/doc/uid/TP40010631-CH1-SW14
Any help is much appreciated.
UPDATE:
Here is a link to a sample Xcode project that illustrates the problem LayoutTest.zip
You need to enable autolayout. If you're using a xib file (and you should be, no commando!) you can check the autolayout checkbox in the Interface Builder Document part of the file inspector.
In code:
[myView setTranslatesAutoresizingMaskIntoConstraints:YES]
In your sample project, you have no constraints. The view won't call layout without any constraints.
NSView *aView = self.window.contentView;
NSDictionary *viewsDictionary=NSDictionaryOfVariableBindings(aView, complexView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"[aView]-[complexView]" options:0 metrics:nil views:viewsDictionary];
for (NSLayoutConstraint *constraint in constraints) {
[complexView addConstraint:constraint];
}
I'm just playing around with OSX app dev. I'm totally noob. I thought it is straight forward like iOS app dev. But after a few days of going at this, it seems it is not that easy.
Here's what I'm trying to do. I have a NSWindow. In it in which I put an Image Well (wth with the naming? lol) Well "Image Well", it is an NSImageView (no pun intended).
So I just want the NSImageView's frame to resize following the NSWindow's size. It's that simple.
Here's what I did that is NOT working:
NSImageView as imageView.
write the delegate method NSWindowDelegate method of NSWindow windowDidResize and windowDidResize: and just resize the framesize of imageView frame of the image view in it. Code: (The NSImageView is in a property called imageView.)
- (void)windowDidResize:(NSNotification *)notification {
// NSLog(#"resized");
NSRect zWindowRect = [[self window]frame];
NSRect zContentRect = [[self window]contentRectForFrameRect:zWindowRect];
[self.imageView setFrameSize:NSMakeSize(zContentRect.size.width,
zContentRect.size.height)];
[self.imageView setNeedsDisplay:YES];
}
This method is called (tested that with NSLog()), however the NSImageView just stays there with the original size. I checked that the IBOutlet connection is OK.
What gives? Why won't the NSImageView resize?
Ok. This is becoming a habit. Answering my own question again.
It seems that this behaviour is due to the "Auto Layout" feature of the Interface Builder.
To fix it, just disable "Auto Layout" in the MainMenu.xib. SEE HERE FOR EXPLANATIONS
In case the site expires: just click on MainMenu.xib, then go to the first tab, File, in the Utilities panel of XCode. And there should be a "Use Auto Layout" checkbox next to it. Uncheck it.
I have a cocoa WebView inside of an NSSplitPane as a subview of one of the split pane's Custom Views. This serves as a preview of some HTML content. To smooth the transition when updating the preview
I make an NSImageView from the WebView
Replace the WebView with the NSImageView
Load the new html into the WebView
Replace the NSImageView with the updated WebView when the html has finished loading
This is the gist of the code is:
From the header
NSImageView *previewImageView;
NSString *content;
#property (strong) IBOutlet NSView *previewContainer;
#property (strong) IBOutlet WebView *previewWebView;
From the class
- (void)updatePreview
{
previewImageView = [self imageViewFromWebView:previewWebView];
[[previewContainer animator] replaceSubview:previewWebView
with:previewImageView];
[[previewWebView mainFrame] loadHTMLString:content baseURL:nil];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[[previewContainer animator] replaceSubview:previewImageView
with:previewWebView];
}
This code does not work correctly if the WebView is defined in the xib file with the referencing outlet set to the previewWebView and the frame load delegate set. The web view is initially shown correctly, gets swapped for the image view ok, but when swapped back does not get displayed.
If I instead define the WebView in code
// inside of viewDidAppear
NSRect frame = [previewContainer frame];
NSRect webViewFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height);
previewWebView = [[WebView alloc] initWithFrame:webViewFrame];
[previewWebView setUIDelegate:self];
[previewWebView setFrameLoadDelegate:self];
[previewWebView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[previewContainer addSubview:previewWebView];
and not the interface builder, the swapping code works as expected. Any ideas as to what may be different about how I'm defining the WebView in code that makes it work but not when define in the interface builder?
It's very possible that Interface Builder archives the WebView object with some settings that are different to the defaults when creating a WebView programmatically.
You should probably try a few things:
[previewWebview setHostWindow:yourWindow];
This associates the WebView with your window. This is required if you remove the WebView from the window, otherwise the WebView will stop operating. The WebView will retain your window, so you should make sure you set the host window to nil before closing your window.
[previewWebView setShouldUpdateWhileOffscreen:YES];
This will ensure the web view actually loads content when it's offscreen.
[previewWebView setShouldCloseWithWindow:NO];
This will prevent the WebView from "closing" when its host window closes. If you don't do this, the WebView will call its close method, which essentially shuts it down, clearing all content and caches and preventing it from being used again. I'm pretty sure this is the default when you instantiate the WebView in Interface Builder, so you want to make sure it doesn't happen.
You may find that you don't need to do this if you've set the host window specifically.
Note that you will need to call [previewWebView close] when you do actually close your window if you do this.
Is it possible to apply custom (added to the app) font to UILabel text via UIBuilder or it can be done only programmatically ?
I have already added
Fonts provided by application
key in application plist
It can only be done programmaticaly, but it is simple: crate a subclass of UILabel and then overwrite the awakeFromNib method:
-(void)awakeFromNib{
[super awakeFromNib];
self.font = [UIFont fontWithName:#"MyFancyFont" size:self.font.pointSize];
}
(with this method you keep the font size you set in IB.)
In the Identity Inspector in IB you have to set the label to the class you created, in "Custom Class" (where it says "UILabel" now).
How do I set the modalViewControll to a particular size?
UINavigationController *NC = (More stuff);
[self presentModelViewController:NC]...;
that code presents my UIViewController as a full screen. I want to to be a smaller window size is that possible or do I need to do a popovercontroller?
If you want something other than full screen you can look at setting the modalPresentationStyle property on the UINavigationController you're about to display.
NC.modalPresentationStyle = UIModalPresentationFormSheet;
Look at the documentation to see what styles are available. If you need something different than those styles then you'll have to do it all yourself. If you use a UIPopoverController you have to be prepared for the user to dismiss your popover by simply tapping elsewhere on the screen.
Check out the modalPresentationStyle property of UIViewController.