博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[iOS]拾遗补阙
阅读量:4973 次
发布时间:2019-06-12

本文共 3755 字,大约阅读时间需要 12 分钟。

一、AFNetworking POST纯字符串

修改位置AFURLRequestSerialization

修改前

NSString * AFQueryStringFromParameters(NSDictionary *parameters) {    NSMutableArray *mutablePairs = [NSMutableArray array];    for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {        [mutablePairs addObject:[pair URLEncodedStringValue]];    }    return [mutablePairs componentsJoinedByString:@"&"];}

修改后

NSString * AFQueryStringFromParameters(NSDictionary *parameters) {    if ([parameters isKindOfClass:[NSString class]])        return (NSString *)parameters;        NSMutableArray *mutablePairs = [NSMutableArray array];    for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {        [mutablePairs addObject:[pair URLEncodedStringValue]];    }    return [mutablePairs componentsJoinedByString:@"&"];}

二、查找一个字符串中的多个相同子字符串

NSString *str = @"14 28 62 24 3 46 99 73 10 51 0 95 34 58 50 33 56 23 97 82 83 8 94 1 17 7 19 96 9 54";NSString *searchString = @"1";NSRange searchRange = NSMakeRange(0, [str length]);NSRange range;while ((range = [str rangeOfString:searchString options:0 range:searchRange]).location != NSNotFound){    NSLog(@"%@",NSStringFromRange(range));        searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));}

三、UITextField限制字数

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    NSInteger existedLength = textField.text.length;    NSInteger selectedLength = range.length;    NSInteger replaceLength = string.length;        if (existedLength - selectedLength + replaceLength > 4)        return NO;        return YES;}

 四、枚举数组元素

[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {    }];

 五、为UILabel添加图片

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];attachment.image = [UIImage imageNamed:@"image"];attachment.bounds = CGRectMake(0, 0, 15, 15);NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];[string appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];[string appendAttributedString:[[NSAttributedString alloc] initWithString:@"string"]];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];label.attributedText = string;[self.view addSubview:label];

 六、自适应屏幕大小

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

 七、判断推送是否打开

BOOL registered = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

 八、UIView转UIImage

- (UIImage *)viewToImage:(UIView *)view{    UIImage *image = [[UIImage alloc] init];    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, [UIScreen mainScreen].scale);// 区域 透明 屏幕密度    [view.layer renderInContext:UIGraphicsGetCurrentContext()];    image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}

 九、跳转App Store

NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=xxx"];[[UIApplication sharedApplication] openURL:url];

 十、UIButton垂直布局

[button setTitleEdgeInsets:UIEdgeInsetsMake(40 ,-button.imageView.frame.size.width, 0, 0)];[button setImageEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, -button.titleLabel.bounds.size.width)];

 十一、解决UITableView在无导航栏时顶部留白

if (@available(iOS 11.0, *))    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;else    self.automaticallyAdjustsScrollViewInsets = NO;

 十二、SDWebImage支持中文路径

[image sd_setImageWithURL:[NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]] forState:UIControlStateNormal];

 

转载于:https://www.cnblogs.com/EverNight/p/10008190.html

你可能感兴趣的文章
Delphi 获取命令行输出的函数
查看>>
面向对象六大原则
查看>>
2-常见机器学习模型总结
查看>>
centos7 安装搜狗输入法
查看>>
本日进度3
查看>>
PyCharm下创建并运行我们的第一个Django项目
查看>>
第十九篇 同源策略与Jsonp
查看>>
python3 调用 Linux 脚本
查看>>
Jquery选中行实现行中的Checkbox的选中与取消选中
查看>>
绘制矩形
查看>>
redis优化
查看>>
vi编辑器的使用(1)
查看>>
微信开发--入门(一)
查看>>
防反编译、混淆文件proguard.cfg与proguard-project.txt详解
查看>>
HTML 第十三章总结
查看>>
2016.12.04软件更新公告
查看>>
深度学习的一些代码
查看>>
在Java窗体表格中插入复选框
查看>>
1230递归下降语法分析程序设计
查看>>
四则运算3
查看>>