我如何加载网页,例如。google.com,然后加载覆盖某些样式的本地 CSS 文件?
第一部分我能做,后一部分我似乎做不到。
像往常一样,答案很简单。
WebPreferences *webPrefs = [WebPreferences standardPreferences];
[webPrefs setUserStyleSheetEnabled:YES];
//Point to wherever your local/custom css is
[webPrefs setUserStyleSheetLocation:[[NSBundle mainBundle] URLForResource:@"style" withExtension:@"css"]];
//Set your webview's preferences
[myWebView setPreferences:webPrefs];
我在这里尝试做同样的事情,除了在我的情况下,我想从我的 OS X 目录中的特定目录加载它。
NSFileManager *fileManager = [NSFileManager defaultManager];
WebPreferences *webPrefs = [WebPreferences standardPreferences];
[webPrefs setUserStyleSheetEnabled:YES];
NSString *folder = @"~/Library/Application Support/irccloud/Style/";
folder = [folder stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath:folder] == NO) {
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:NULL];
}
//Point to wherever your local/custom css is
NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/ali/Library/Application Support/irccloud/Style/style.css"];
[webPrefs setUserStyleSheetLocation:fileURL];
//Set your webview's preferences
[webView setPreferences:webPrefs];
在斯威夫特:
// Now kick start css
let ourWebPrefs:WebPreferences = WebPreferences.standardPreferences();
ourWebPrefs.userStyleSheetEnabled = true;
ourWebPrefs.userStyleSheetLocation = NSBundle.mainBundle().URLForResource("webview", withExtension: "css");
customWebView.preferences = ourWebPrefs;