Basic knowledge of IOS development-fragment 37 and basic knowledge of ios-37
1: iOS uses NJKWebViewProgress for webview progress bars
Import the header file: # import "NJKWebViewProgressView. h" # import "NJKWebViewProgress. h"
Comply with the Protocol <UIWebViewDelegate, authorization> implementation code @ implementation ViewController {IBOutlet _ weak UIWebView * _ webView; authorization * _ 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-progress-(void) webViewProgress :( NJKWebViewProgress *) webViewProgress updateProgress :( float) progress {[_ webViewProgressView setProgress: progress animated: YES]; self. title = [_ webView stringByEvaluatingJavaScriptFromString: @ "document. title "];}
2: solve the problem that the UITextField in the input box responds to the pinyin or radical as content.
// Add a listener [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (textFieldDidChanged :) name: UITextFieldTextDidChangeNotification object: self. textField]; // listener processing-(void) textFieldDidChanged :( NSNotification *) notification {NSString * text = self. textField. text; // when the Pinyin is entered, the pinyin letter is selected. At this time, it is not determined whether the UITextRange * selectedRange = [self. textField markedTextRange]; if (! SelectedRange |! SelectedRange. start) {if (text. length> MAXLENGTH) {self. textField. text = [text substringToIndex: MAXLENGTH] ;}}
Here we mainly use two knowledge:
- When the input method is used, the pinyin letters or strokes are selected and can be obtained using markedTextRange.
- UITextFieldTextDidChangeNotification is triggered when you enter common input and the words to be selected in the input box. You can listen to this notification and clear the UITextField content afterwards.
In addition to listening to NSNotification, you can also use addTargetAction. The Code is as follows:
[self.textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
This is equivalent to the above method, but NSNotification requires removeObserver when the object is destroyed.
3: Adjust the UIButton size according to the content
NSString * str = @ "this is the button title"; UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. titleLabel. font = [UIFont systemFontOfSize: 13.0]; // you have set the shape of the button ~ 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]; // The following is important! CGSize titleSize = [str sizeWithAttributes: @ {NSFontAttributeName: [UIFont fontWithName: btn. titleLabel. font. fontName size: btn. titleLabel. font. pointSize]}]; titleSize. height = 20; titleSize. width + = 20; btn. frame = CGRectMake (100,100, titleSize. width, titleSize. height); [view addSubview: btn];
4: Solutions for different text styles
NSRange range = [title rangeOfString:targetname]; NSMutableAttributedString *mutableTitle = [[NSMutableAttributedString alloc] initWithString:title]; [mutableTitle addAttribute:NSForegroundColorAttributeName value:COLOR_WORD_GRAY_2 range:NSMakeRange(0, title.length)]; if (range.length>0) { [mutableTitle addAttribute:NSForegroundColorAttributeName value:HEXCOLOR(0xf7ab00) range:range]; NSInteger startIndex = range.location+range.length; [mutableTitle addAttribute:NSForegroundColorAttributeName value:COLOR_WORD_GRAY_2 range:NSMakeRange(startIndex, title.length-startIndex)]; [mutableTitle addAttribute:NSFontAttributeName value:CHINESE_SYSTEM(16) range:NSMakeRange(0, title.length)]; } self.titleLabel.attributedText=mutableTitle;