iOS之常用的方法和技巧
【摘要】
判断字符串是不是以中文开头
- (BOOL)isChineseFirst:(NSString *)firstString {
//是否以中文开头(unicode中文编码范围是0x4e00~0x...
- 判断字符串是不是以中文开头
- (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->建议获取的范围,可以传递NULL
BOOL isChinese = [firstString getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
if (isChinese && (utfCode >= 0x4e00 && utfCode <= 0x9fa5))
return YES;
else
return NO;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 键盘回收
- (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];
}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 禁止侧滑返回上一个界面
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
- 1
- 2
- 3
- 隐藏导航栏最下面的黑线
- (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;
}
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 去除自带的searchBar的背景View
- (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;
}
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 标签栏的背景图显示错乱问题解决办法:
//将图片如下设置
[[UIImage imageNamed:@"me@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
- 1
- 2
- 网络请求设置超时请求(基于AFNetworking3.0封装的GET,POST请求用方法)
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 15.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
- 1
- 2
- 3
- 4
- 实用的获取图片的方法,防止内存溢出:
// 建议使用该方法:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"图片" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
// 不建议使用该方法:
UIImage *image = [UIImage imageNamed:@"图片.png"];
- 1
- 2
- 3
- 4
- 5
- 6
文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Forever_wj/article/details/53996489
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)