生活随笔
收集整理的這篇文章主要介紹了
iOS之常用的方法和技巧
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- (BOOL)isChineseFirst:(NSString *)firstString {//是否以中文開頭(unicode中文編碼范圍是0x4e00~0x9fa5)int utfCode = 0;void *buffer = &utfCode;NSRange range = NSMakeRange(0, 1);//判斷是不是中文開頭的,buffer->獲取字符的字節數據 maxLength->buffer的最大長度 usedLength->實際寫入的長度,不需要的話可以傳遞NULL encoding->字符編碼常數,不同編碼方式轉換后的字節長是不一樣的,這里我用了UTF16 Little-Endian,maxLength為2字節,如果使用Unicode,則需要4字節 options->編碼轉換的選項,有兩個值,分別是NSStringEncodingConversionAllowLossy和NSStringEncodingConversionExternalRepresentation range->獲取的字符串中的字符范圍,這里設置的第一個字符 remainingRange->建議獲取的范圍,可以傳遞NULLBOOL isChinese = [firstString getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];if (isChinese && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))return YES;elsereturn NO;
}
- (void)setUpForDismissKeyboard {UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)];NSOperationQueue *operation = [NSOperationQueue mainQueue];[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {[self.view addGestureRecognizer:tapGesture];}];[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:operation usingBlock:^(NSNotification * _Nonnull note) {[self.view removeGestureRecognizer:tapGesture];}];
}
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {self.navigationController.interactivePopGestureRecognizer.enabled = NO;}
- (void)hiddenNavLine {if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){NSArray *list=self.navigationController.navigationBar.subviews;for (id obj in list) {if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {UIView *view = (UIView*)obj;for (id obj2 in view.subviews) {if ([obj2 isKindOfClass:[UIImageView class]]) {UIImageView *image = (UIImageView*)obj2;image.hidden = YES;}}}}}
}
- (void)removeSearchBarBackGroundView {for(int i = 0 ;i < _searchBar.subviews.count;i++){UIView * backView = _searchBar.subviews[i];if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {[backView removeFromSuperview];[_searchBar setBackgroundColor:[UIColor clearColor]];break;} else {NSArray * arr = _searchBar.subviews[i].subviews;for(int j = 0;j < arr.count; j++){UIView * barView = arr[i];if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {[barView removeFromSuperview];[_searchBar setBackgroundColor:[UIColor clearColor]];break;}}}}
}
//將圖片如下設置
[[UIImage imageNamed:@"me@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
- 網絡請求設置超時請求(基于AFNetworking3.0封裝的GET,POST請求用方法)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];manager.requestSerializer.timeoutInterval = 15.f;[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 建議使用該方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"圖片" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];// 不建議使用該方法:
UIImage *image = [UIImage imageNamed:@"圖片.png"];
總結
以上是生活随笔為你收集整理的iOS之常用的方法和技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。