UIWebView背景顏色的設置 webview加載html文本 禁用webview長按

最近使用了UIWebView,發現當如下設置時

    myWebView.backgroundColor = [UIColor clearColor];
沒有實現預期的透明效果

後來加上

myWebView.opaque = NO;
myWebView.backgroundColor = [UIColor clearColor];

OK!  webView 實現了透明


webview 加載html文本 

-(void)addWebView
{
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 10, 300, 200)];
    webView.opaque = NO;
    [self.view addSubview:webView];
    webView.backgroundColor = [UIColor cleanColor];
    NSString *ring = @"<h3>請客劵是什麼?</h3><p><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\">請客劵是網站推送的拉動用戶註冊的一種優惠措施!</span><br/></p><h3><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\"><span style=\"font-size: 14px; background-color: rgb(255, 255, 255);\">請客劵有什麼類型?</span><br/></span></h3><p><span style=\"font-size: 14px; background-color: rgb(0, 176, 240);\"><span style=\"font-size: 14px; background-color: rgb(255, 255, 255);\"><em>請客劵分爲www.ifood517.com<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 112, 192);\">限非會員<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\">和<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 112, 192);\">會員之分<span style=\"font-size: 14px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);\">!</span></span></span></span></em><br/></span></span></p>";
    [webView loadHTMLString:ring baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]];
    webView.delegate = self;
}


webview 長按 會出現一些選擇項 如何禁用長按

- (void)webViewDidFinishLoad:(UIWebView *)webView {

   // 禁用用戶選擇
   [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

   // 禁用長按彈出框
   [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

____________________________________________  華麗麗的分割線  ____________________________________________


關於禁用長按webview後出現的功能項 最近有一個新的方法 

用手勢替換  感覺也是不錯的


//添加手勢 替換掉webView長按出現的功能選項
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
    longPress.delegate = self;
    longPress.minimumPressDuration = 0.4; //個人測試感覺0.4秒是最合適的時間
    [webView addGestureRecognizer:longPress];


webView下拉會出現陰影(iOS7.0以下系統)  怎麼辦呢?

cmd + UIWebView    看看關於UIWebView的API 

@property(nonatomic,readonly,retain) UIScrollView *scrollView NS_AVAILABLE_IOS(5_0);


webview上其實是一個scrollview

[webView subviews] objectAtIndex:0];  //  這樣就可以找到它了 



然後 想避免出現下拉陰影   方法之一就是禁掉 scrollview的回彈

[(UIScrollView *)[[webView_ subviews] objectAtIndex:0] setBounces:NO];

方法之二

 for (UIView *subView in [webView_ subviews]) {
        if ([subView isKindOfClass:[UIScrollView class]]) {
            for (UIView *shadowView in [subView subviews]) {
                if ([shadowView isKindOfClass:[UIImageView class]]) {
                    shadowView.hidden = YES;
                }
            }
        }
    }


ok  下班!



發佈了28 篇原創文章 · 獲贊 5 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章