iOS开发经验

修改textField的placeholder的字体颜色、大小

self.textField.placeholder = @"username is in here!"; [self.textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [self.textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

(使用attributedString进行设置)
NSString *string = @"美丽新世界"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

[attributedStringaddAttribute: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;

关闭/收起键盘方法总结

1、点击Return按扭时收起键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignFirstResponder]; }
2、点击背景View收起键盘
[self.view endEditing:YES];
3、你可以在任何地方加上这句话,可以用来统一收起键盘
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

线程中更新 UILabel的text

[self.label1 performSelectorOnMainThread:@selector(setText:) withObject:textDisplaywaitUntilDone:YES];

获得当前硬盘空间

NSFileManager *fm = [NSFileManager defaultManager]; NSDictionary *fattributes =[fmattributesOfFileSystemForPath:NSHomeDirectory() error:nil];
NSLog(@"容量%lldG",[[fattributes objectForKey:NSFileSystemSize] longLongValue]/1000000000); NSLog(@"可用%lldG",[[fattributes objectForKey:NSFileSystemFreeSize] longLongValue

给UIView 设置透明度,不影响其他subviews

(设置background color的颜色中的透明度)
[self.testView setBackgroundColor:[UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.5]];

iOS加载启动图的时候隐藏statusbar

只需需要在info.plist中加入Status bar is initially hidden 设置为YES就好


1493786152783017.jpg

iOS 开发,工程中混合使用 ARC 和非ARC

Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式。

如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。

如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。

添加标签的方法:
打开:你的target -> Build Phases -> Compile Sources.
双击对应的 *.m 文件
在弹出窗口中输入上面提到的标签 -fobjc-arc / -fno-objc-arc
点击 done 保存

在UIViewController中property的一个UIViewController的Present问题

如果在一个UIViewController A中有一个property属性为UIViewController B,实例化后,将BVC.view 添加到主UIViewController A.view上,如果在viewB上进行 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_AVAILABLE_IOS(5_0);的操作将会出现,“ Presenting view controllers on detached view controllers is discouraged ” 的问题。

以为BVC已经present到AVC中了,所以再一次进行会出现错误。
可以使用
[self.view.window.rootViewController presentViewController:imagePickeranimated:YES completion:^{ NSLog(@"Finished"); }];

ActivityViewController 使用AirDrop分享

使用AirDrop 进行分享:
NSArray *array = @[@"test1", @"test2"]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];

[self presentViewController:activityVC animated:YES completion:^{ NSLog(@"Air"); }];

非空判断注意

BOOL hasBccCode = YES; if ( nil == bccCodeStr || [bccCodeStr isKindOfClass:[NSNull class]] || [bccCodeStr isEqualToString:@""]) { hasBccCode = NO; }

模拟器中文输入法设置

模拟器默认的配置种没有“小地球”,只能输入英文。加入中文方法如下:
选择Settings--->General-->Keyboard-->International KeyBoards-->Add New Keyboard-->Chinese Simplified(PinYin) 即我们一般用的简体中文拼音输入法,配置好后,再输入文字时,点击弹出键盘上的“小地球”就可以输入中文了。

如果不行,可以长按“小地球”选择中文。

微信分享的时候注意大小

text 的大小必须 大于0 小于 10k

image 必须 小于 64k

url 必须 大于 0k

图片缓存的清空

// 清理内存
[[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]; }];

JSON的“” 转换为nil

使用AFNetworking 时, 使用
AFJSONResponseSerializer *response = [[AFJSONResponseSerializer alloc] init]; response.removesKeysWithNullValues = YES;
_sharedClient.responseSerializer = response;

pragma mark - 不能输入汉字和特殊字符的 的密码

+ (BOOL)validatePwd:(NSString *)param {
NSString *pwdRegex = @"^[a-zA-Z0-9]+$"; NSPredicate *pwdPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pwdRegex]; return [pwdPredicate evaluateWithObject:param]; }

#pragma mark - 验证金额
+ (BOOL)checkPrice:(NSString *)price {
/*
1.可以输入小数点后两位

  1. (1.)这种情况要避免
  2. 只能输入非负整数
    4.第一位是0,第二位不能是0
    /
    //^([1-9]\d
    |0)(.\d?[1-9])?$ 金额
    //@"^(0?[1-9]\d*|0)(\.\d?[1-9])?$"; 可01
    NSString *pattern = @"^([1-9]\\d*|0)(\\.\\d?[1-9])?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:price];
    return isMatch;}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容