设为首页 加入收藏

TOP

IOS开发基础知识--碎片37(一)
2017-10-13 10:23:57 】 浏览:4100
Tags:IOS 开发 基础知识 碎片

1:iOS 使用NJKWebViewProgress做webview进度条

引入头文件:
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
遵守协议
<UIWebViewDelegate, NJKWebViewProgressDelegate>

实现代码
@implementation ViewController
{
    IBOutlet __weak UIWebView *_webView;
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;


CGRect navBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0,
                             navBounds.size.height - 2,
                             navBounds.size.width,
                             2);
_webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
_webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[_webViewProgressView setProgress:0 animated:YES];
[self loadBaidu];
[self.navigationController.navigationBar addSubview:_webViewProgressView];
}

-(void)loadBidu
{
    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
    [_webView loadRequest:req];
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
    self.title = [_webView stringByeva luatingjava scriptFromString:@"document.title"];
}

 2:解决输入框UITextField关于拼音或部首被当作内容响应

// 添加监听
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldDidChanged:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:self.textField];

// 监听处理
- (void)textFieldDidChanged:(NSNotification *)notification {
    NSString *text = self.textField.text;

    // 拼音输入时,拼音字母处于选中状态,此时不判断是否超长
    UITextRange *selectedRange = [self.textField markedTextRange];
    if (!selectedRange || !selectedRange.start) {
        if (text.length > MAXLENGTH) {
            self.textField.text = [text substringToIndex:MAXLENGTH];
        }
    }
}

这里主要使用了两个知识:

  • 输入法输入时,拼音字母或者笔画处于选中状态,可以使用 markedTextRange 获取到
  • 普通输入,以及将输入法的待选字填入输入框时,都会发出 UITextFieldTextDidChangeNotification, 可以监听这个通知,并事后对 UITextField 的内容做清理

另外除了使用监听 NSNotification 的方式,也可以使用 addTargetAction 的方式,代码如下:

[self.textField addTarget:self
                   action:@selector(textChange:)
         forControlEvents:UIControlEventEditingChanged];

这与上面的方式是等价的,但是使用 NSNotification 需要在对象销毁时 removeObserver,而这种方式不需要

3:根据内容自适应UIButton的大小

NSString *str = @"这是按钮的标题";
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont systemFontOfSize:13.0];
//对按钮的外形做了设定,不喜可删~
    btn.layer.masksToBounds = YES;
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = [[UIColor blackColor] CGColor];
    btn.layer.cornerRadius = 3;

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:str forState:UIControlStateNormal];

//重要的是下面这部分哦!
    CGSize titleSize = [str sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:btn.titleLabel
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[iOS]开发之-字典转模型和KVC 下一篇iOS中的小知识点

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目