UIWebView UITextView

//
//  ViewController.m
//  网页
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFiled;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _textFiled.delegate = self;
    _webView.delegate = self;
    
    _textFiled.returnKeyType = UIReturnKeySearch;
    //网页缩放
    _webView.scalesPageToFit = YES;
    
    [self loadTextFiledUrlString];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)loadTextFiledUrlString{
    NSURL *url = [NSURL URLWithString:_textFiled.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    
}

#pragma mark -UITextFiledDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    //加载网址
    [self loadTextFiledUrlString];
    //失去第一响应
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark -UIWebViewDelegate

//是否允许开始加载网络请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

//已经加载请求
-(void)webViewDidStartLoad:(UIWebView *)webView{
    //显示风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
//加载请求成功
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
//加载请求失败
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    //加载html (走马灯)
    NSString *htmlString = @"<html><marquee><h1>对不起,网络加载请求失败 !</h1></marquee></html>";
    [_webView loadHTMLString:htmlString baseURL:nil];
}

/* -- UIWebView
 //常用属性
 id <UIWebViewDelegate> delegate ;   //设置控件的委托
 NSURLRequest *request //webView网络请求
 BOOL scalesPageToFit ; //根据屏幕自适应
 BOOL canGoBack ; //可以跳转上一页
 BOOL canGoForward ; //可以跳转下一页
 BOOL loading ; //正在加载中
 
 //常用方法
 - (void)loadRequest:(NSURLRequest *)request;/加载网络请求
 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;//加载本地HTML
 - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;//加载缓存数据
 - (void)reload;//刷新当前页
 - (void)stopLoading;//停止加载当前页
 - (void)goBack;//返回上一页
 - (void)goForward;//跳转下一页
 - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;//使用js脚本
 
 //代理方法
 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;//允许开始加载网络请求
 - (void)webViewDidStartLoad:(UIWebView *)webView;//已经加载请求
 - (void)webViewDidFinishLoad:(UIWebView *)webView;//加载请求成功
 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;//加载请求失败
 */

/* --UITextView
 //常用属性
 d <UITextViewDelegate> delegate ;   //设置UITextView的委托
 NSString *text;  //控件上面的显示的文字
 UIFont *font ; //文字字体大小
 UIColor *textColor ; //字体颜色
 NSTextAlignment textAlignment ; //文字对齐方式
 NSRange *selectedRange ; //选中文字的range
 NSAttributedString *attributedText ; //属性字符串
 
 //常用委托方法
 - (BOOL)textViewShouldBeginEditing:(UITextView *)textView;//允许开始编辑
 - (BOOL)textViewShouldEndEditing:(UITextView *)textView;//允许结束编辑
 - (void)textViewDidBeginEditing:(UITextView *)textView;//已经开始编辑
 - (void)textViewDidEndEditing:(UITextView *)textView;//已经结束编辑
 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;//允许更改文本
 - (void)textViewDidChange:(UITextView *)textView;//文本已经更改
 */

@end

 

posted @ 2016-04-15 10:01  麦兜丶  阅读(204)  评论(0编辑  收藏  举报