1、设置navigationBar title的颜色
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];
[self.navigationController.navigationBar setTitleTextAttributes:dic];
2、修改textField的placeholder的字体颜色、大小
a.通过kvo设置
self.textField.placeholder = @"请输入商品名称";
[self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
b.通过富文本设置
NSString *string = @"请输入商品名称";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, [string length])];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16]
range:NSMakeRange(0, [string length])];
self.textField.attributedPlaceholder = attributedString;
3、关闭/收起键盘方法
a.点击Return按扭时收起键盘
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
return [textField resignFirstResponder];
}
b.点击背景View收起键盘
[self.view endEditing:YES];
c.在任何地方统一收起键盘
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
4、将color转为UIImage
-(UIImage*)createImageWithColor:(UIColor*)color{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
5、NSString进行URL编码转换
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
6、清理内存
// 清理内存
[[SDImageCache sharedImageCache] clearMemory];
// 清理webview 缓存
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config.URLCache removeAllCachedResponses];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// 清理硬盘
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self.tableView reloadData];
}];
7、返回一个没有重复元素的数组
-(NSArray *)arrayWithMemberIsOnly:(NSArray *)array{
NSMutableArray *categoryArray = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [array count]; i++) {
if ([categoryArray containsObject:[array objectAtIndex: i]] == NO) {
[categoryArray addObject:[array objectAtIndex: i]];
}
}
return categoryArray;
}
8、限制UITextField字数
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField.text.length + string.length > 18) {
return NO;
}
if (textField.text.length < range.location + range.length) {
return NO;
}
return YES;
}
9、隐私数据访问问题
项目中访问了隐私数据,比如:相机,相册,联系人等,在Xcode8中打开编译的话,统统会crash.提交App Store会被拒绝,可以才info.plist中添加如下设置:
<!-- 相册 -->
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<!-- 相机 -->
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<!-- 麦克风 -->
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能访问麦克风</string>
<!-- 位置 -->
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<!-- 在使用期间访问位置 -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
<!-- 始终访问位置 -->
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<!-- 日历 -->
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能访问日历</string>
<!-- 提醒事项 -->
<key>NSRemindersUsageDescription</key>
<string>App需要您的同意,才能访问提醒事项</string>
<!-- 运动与健身 -->
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能访问运动与健身</string>
<!-- 健康更新 -->
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能访问健康更新 </string>
<!-- 健康分享 -->
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能访问健康分享</string>
<!-- 蓝牙 -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能访问蓝牙</string>
<!-- 媒体资料库 -->
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>
10、银行卡校验码算法
-(void)getBankCardCheckCode{
NSString *cardNumber = @"625965087177209";
NSMutableString *checkCode = [NSMutableString string];
//校验码为银行卡号最后一位,采用LUHN算法,亦称模10算法。计算方法如下:
for (NSInteger i = 0; i < cardNumber.length; i+=2) {
//第一步:从右边第1个数字开始每隔一位乘以2;
NSString *str = [cardNumber substringWithRange:NSMakeRange(i, 1)];
[checkCode appendString:[NSString stringWithFormat:@"%ld", [str integerValue] * 2]];
}
NSInteger sum = 0;
for (NSInteger i = 0; i < checkCode.length; i++) {
//第二步: a.把在第一步中获得的乘积的各位数字相加,然
NSInteger number = [[checkCode substringWithRange:NSMakeRange(i, 1)] integerValue];
sum += number;
}
for (NSInteger i = 1; i < cardNumber.length; i+=2) {
//b.后再与原号码中未乘2的各位数字相加;
NSInteger number = [[cardNumber substringWithRange:NSMakeRange(i, 1)] integerValue];
sum += number;
}
//第三步:对于第二步求和值中个位数求10的补数,如果个位数为0则该校验码为0。
NSInteger code = 10 - (sum%10);
NSString *fullCardNumber = [NSString stringWithFormat:@"%@%ld", cardNumber, code];
NSLog(@"fullCardNumber:%@", fullCardNumber);
}
11、设置label文字属性,一半斜体一半sys,一半红色,一半黑色
_starGoodsLabel = [[UILabel alloc] init];
CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(15 * (CGFloat)M_PI / 180), 1, 0, 0);
UIFontDescriptor *desc = [ UIFontDescriptor fontDescriptorWithName :[ UIFont systemFontOfSize :17 ]. fontName matrix :matrix];
_starGoodsLabel.font = [ UIFont fontWithDescriptor :desc size :17];
_starGoodsLabel.textColor = [UIColor redColor];
NSString *string = @"/ 底价秒杀 / 人人都在抢的货";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:string];
[str addAttribute:NSForegroundColorAttributeName value:RGB(68, 68, 68) range:NSMakeRange(10, string.length - 10)];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(10, string.length - 10)];
_starGoodsLabel.attributedText = str;
[view addSubview:_starGoodsLabel];
[_starGoodsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.contentView.mas_left).offset(10);
make.right.mas_equalTo(self.contentView.mas_right).offset(-120);
make.top.mas_equalTo(self.contentView.mas_top).offset(7);
make.height.equalTo(@14);
}];
12、截图
//截取view生成一张图片
+ (UIImage *)shotWithView:(UIView *)view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//截取view中某个区域生成一张图片
+ (UIImage *)shotWithView:(UIView *)view scope:(CGRect)scope{
CGImageRef imageRef = CGImageCreateWithImageInRect([self shotWithView:view].CGImage, scope);
UIGraphicsBeginImageContext(scope.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, scope.size.width, scope.size.height);
CGContextTranslateCTM(context, 0, rect.size.height);//下移
CGContextScaleCTM(context, 1.0f, -1.0f);//上翻
CGContextDrawImage(context, rect, imageRef);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(imageRef);
CGContextRelease(context);
return image;
}
13、日期与时间戳互转
//将时间戳转换为NSDate类型
-(NSDate*)getDateTimeFromMilliSeconds:(long long)miliSeconds{
NSTimeInterval tempMilli = miliSeconds;
NSTimeInterval seconds = tempMilli/1000.0;//这里的.0一定要加上,不然除下来的数据会被截断导致时间不一致
NSLog(@"传入的时间戳=%f", seconds);
return [NSDate dateWithTimeIntervalSince1970: seconds];
}
//将NSDate类型的时间转换为时间戳,从1970/1/1开始
-(long long)getDateTimeTOMilliSeconds:(NSDate*)datetime{
NSTimeInterval interval = [datetime timeIntervalSince1970];
NSLog(@"转换的时间戳=%f", interval);
long long totalMilliseconds = interval*1000;
NSLog(@"totalMilliseconds=%llu", totalMilliseconds);
return totalMilliseconds;
}
14、指纹识别返回码
a、 验证(指纹/密码)不能开启的错误信息(指纹系统被判定为无效):
LAErrorPasscodeNotSet : 设备密码未设置
LAErrorTouchIDNotAvailable : TOUCH ID不可用
LAErrorTouchIDNotEnrolled : 指纹未录入
LAErrorTouchIDLockout : TOUCH ID被锁定
LAErrorAppCancel : APP调用了- (void)invalidate方法使LAContext失效
LAErrorInvalidContext : 实例化的LAContext对象失效,再次调用evaluation...方法则会弹出此错误信息
b、 其他错误信息(指纹系统判定有效,但是验证指纹错误):
LAErrorAuthenticationFailed : 鉴定失败
LAErrorUserCancel : 用户取消
LAErrorUserFallback : 用户选择输入密码
LAErrorSystemCancel : 系统取消(如:另外一个应用进入前台)
15、解决tableView分隔线少一段的方法
在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero
能将空白去掉。
但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero
已经不起作用了。下面是解决办法
首先在viewDidLoad方法加入以下代码:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
然后在UITableView的代理方法中加入以下代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}