*- 撿起初心铃剔, 慢慢走,希望自己好好努力查刻,志于不用工作的人 *
1键兜、禁止第三方鍵盤
在要填密碼的地方, 為了安全考慮穗泵!要么禁用第三方鍵盤普气,要么自定義鍵盤!
//禁止第三方鍵盤的使用
- (BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier{
return NO;
}
2佃延、禁止系統(tǒng)的面板
手機(jī)默認(rèn)的輸入框右鍵或者選中文字右鍵會(huì)彈出菜單现诀,但是有時(shí)候我們?cè)谧鰧?duì)文本嚴(yán)格要求的時(shí)候 (比如說不能使文字啊),就要關(guān)閉這個(gè)功能履肃, 只需要在自定義的UITextfield或UITextview里面添加下面這一句話:
3.36.57.png
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
UIMenuController * menu = [UIMenuController sharedMenuController];
if (menu) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
3赶盔、點(diǎn)擊部分文字響應(yīng)
有時(shí)候你需要在一行文本中點(diǎn)擊某幾個(gè)特殊的文字,來實(shí)現(xiàn)跳轉(zhuǎn)網(wǎng)頁或者打電話的功能榆浓。我的需求是在公告里面要能點(diǎn)擊電話。
4.00.39.png
我是自定義了一個(gè)UITextView實(shí)現(xiàn)得撕攒,代碼如下:
@implementation FDTextKitVIew
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
}
return self;
}
- (void)setText:(NSString *)text{
[super setText:text];
//目標(biāo)文字是你已知的文字陡鹃,或者取服務(wù)器字段知道的
NSRange range = [self.text rangeOfString:目標(biāo)文字];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:5];
// 行間距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
//改變電話號(hào)碼的背景色
[attributedString addAttribute:NSForegroundColorAttributeName value:DRHColor(48, 121, 255) range:range];
// 下劃線
[attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:16] range:NSMakeRange(0, text.length)];
self.attributedText = attributedString;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSSet *allTouches = [event allTouches]; //返回與當(dāng)前接收者有關(guān)的所有的觸摸對(duì)象
UITouch *touch = [allTouches anyObject]; //視圖中的所有對(duì)象
CGPoint point = [touch locationInView:[touch view]];
NSRange range = [self.text rangeOfString:目標(biāo)文字];
// self.selectedRange = range;
// 不能設(shè)置是因?yàn)?selectedable
[self setSelectedRange:range];
NSArray * arr = [self selectionRectsForRange:self.selectedTextRange];
for(UITextSelectionRect * textrect in arr){
if(CGRectContainsPoint(textrect.rect, point)){
// 這里就是響應(yīng)的地方
}
}
}
@end
4烘浦、獲取網(wǎng)絡(luò)圖片的尺寸
Paste_Image.png
以前做這種像帖子詳情或想新浪微博的布局時(shí)用的不是h5, 都是前端要自己獲取圖片數(shù)組萍鲸,自己計(jì)算圖片的寬高做自適應(yīng)闷叉, 當(dāng)時(shí)幸虧找到一個(gè)大神寫的工具類,全部都是對(duì)二進(jìn)制字節(jié)流的計(jì)算脊阴。
現(xiàn)在常用這個(gè)一個(gè)方法:
NSArray * urlArr = @[
@"http://upload-images.jianshu.io/upload_images/31282-390513513494ede8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",
@"http://upload-images.jianshu.io/upload_images/1931381-12109e9d6666bb0e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",
@"http://upload-images.jianshu.io/upload_images/3375207-6adc6a22441681ce.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",
@"http://upload-images.jianshu.io/upload_images/3375207-6adc6a22441681ce.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240",
@"http://upload-images.jianshu.io/upload_images/1336788-2ac84707ce22e970.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1080/q/50"];
dispatch_group_t grounp = dispatch_group_create();
for(NSString * str in urlArr){
// 將當(dāng)前的下載操作添加到組中
dispatch_group_enter(grounp);
// 緩存圖片
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:str] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
// 把緩存到本地的img添加到數(shù)據(jù)握侧, 也可以通過image.size取到寬高比例
[_arr addObject:image];
//完成單張圖片下載離開當(dāng)前組
dispatch_group_leave(grounp);
}];
}
dispatch_group_notify(grounp, dispatch_get_main_queue(), ^{
// 來到這里所有圖片都下載完成
// 更新UI, 就好了
});
5、清除webviewde的緩存
- (void)dealloc{
self.m_WebView = nil;
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]){
[storage deleteCookie:cookie];
}
//清除UIWebView的緩存
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
[cache setMemoryCapacity:0];
}