1.TextField输入金额限制
#define myDotNumbers @"0123456789.\n"
#define myNumbers @"0123456789\n"
// textField输入金额的限制,小数点前9位,后面两位,如果不加小数点,最大位数是9位,加上小数点,最大位数是12位,超出最大位数可删除
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *) string {
// 判断是否输入内容,或者用户点击的是键盘的删除按钮
if (![string isEqualToString:@""]) {
NSCharacterSet *cs;
// 小数点在字符串中的位置 第一个数字从0位置开始
NSInteger dotLocation = [textField.text rangeOfString:@"."].location;
// 判断字符串中是否有小数点,并且小数点不在第一位
// NSNotFound 表示请求操作的某个内容或者item没有发现,或者不存在
// range.location 表示的是当前输入的内容在整个字符串中的位置,位置编号从0开始
if (dotLocation == NSNotFound && range.location != 0) {
// 取只包含“myDotNumbers”中包含的内容,其余内容都被去掉
/* [NSCharacterSet characterSetWithCharactersInString:myDotNumbers]的作用是去掉"myDotNumbers"中包含的所有内容,只要字符串中有内容与"myDotNumbers"中的部分内容相同都会被舍去在上述方法的末尾加上invertedSet就会使作用颠倒,只取与“myDotNumbers”中内容相同的字符
*/
cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];
if (range.location >= 9) {
NSLog(@"单笔金额不能超过亿位");
if ([string isEqualToString:@"."] && range.location == 9) {
return YES;
}
return NO;
}
}else {
cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];
}
// 按cs分离出数组,数组按@""分离出字符串
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOL basicTest = [string isEqualToString:filtered];
if (!basicTest) {
NSLog(@"只能输入数字和小数点");
return NO;
}
if (dotLocation != NSNotFound && range.location > dotLocation + 2) {
NSLog(@"小数点后最多两位");
return NO;
}
if (textField.text.length > 11) {
return NO;
}
}
return YES;
}
2 拖拽约束实现布局动画
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * orderViewHeight;
self.orderViewHeight.constant = 110;
[self.orderView setNeedsUpdateConstraints];
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:0.4 animations:^{
[weakSelf.orderView layoutIfNeeded];
}]
3 打电话
NSString *phoneNum = @"15201269722";
NSString *urlStr = [NSString stringWithFormat:@"tel://%@",phoneNum];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication]openURL:url];
4 iOS提交TestFlight测试显示缺少合规证明
在info.plist中增加键值对,ITSAppUsesNonExemptEncryption 设置为NO
5 相机调用后为英文
解决:info.plist里面添加Localized resources can be mixed YES(表示是否允许应用程序获取框架库内语言)
6.获取当前屏幕显示的viewcontroller
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
return currentVC;
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
// 视图是被presented出来的
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
// 根视图为UITabBarController
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]){
// 根视图为UINavigationController
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
// 根视图为非导航类
currentVC = rootVC;
}
return currentVC;
}
7. 内存警告问题
- ARC模式下 CGImage 释放问题
内存泄漏警告如下:
- ARC模式下 CGImage 释放问题
在ARC模式下 不是什么东西 都可以释放 例如 C-types的对象 都需要手动来进行释放
手动释放 CGImageRelease(subImageRef);
- 使用 SCNetworkReachability和Reachability监测网络连接状态时,调用CFRelease 来释放内存警告的地方
8. 字符串转UTF-8
NSString *str1 = [str stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
//转回去 NSString *str2 = [str1 stringByRemovingPercentEncoding];
9.监听状态栏高度改变的通知(通话或录音状态状态栏多出20)
UIApplicationDidChangeStatusBarFrameNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutControllerSubViews) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
或者使用
- (void)viewWillLayoutSubviews
- (void)viewDidLayoutSubviews
10.数字金额转大写金额
-(NSString *)digitUppercaseWithMoney:(NSString *)money
{
NSMutableString *moneyStr=[[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%.2f",[money doubleValue]]];
NSArray *MyScale=@[@"分", @"角", @"元", @"拾", @"佰", @"仟", @"万", @"拾", @"佰", @"仟", @"亿", @"拾", @"佰", @"仟", @"兆", @"拾", @"佰", @"仟" ];
NSArray *MyBase=@[@"零", @"壹", @"贰", @"叁", @"肆", @"伍", @"陆", @"柒", @"捌", @"玖"];
NSArray *integerArray = @[@"拾", @"佰", @"仟", @"万", @"拾万", @"佰万", @"仟万", @"亿", @"拾亿", @"佰亿", @"仟亿", @"兆", @"拾兆", @"佰兆", @"仟兆"];
NSMutableString * M = [[NSMutableString alloc] init];
[moneyStr deleteCharactersInRange:NSMakeRange([moneyStr rangeOfString:@"."].location, 1)];
for(NSInteger i=moneyStr.length;i>0;i--)
{
NSInteger MyData=[[moneyStr substringWithRange:NSMakeRange(moneyStr.length-i, 1)] integerValue];
[M appendString:MyBase[MyData]];
//判断是否是整数金额
if (i == moneyStr.length) {
NSInteger l = [[moneyStr substringFromIndex:1] integerValue];
if (MyData > 0 &&
l == 0 ) {
NSString *integerString = @"";
if (moneyStr.length > 3) {
integerString = integerArray[moneyStr.length-4];
}
[M appendString:[NSString stringWithFormat:@"%@%@",integerString,@"元整"]];
break;
}
}
if([[moneyStr substringFromIndex:moneyStr.length-i+1] integerValue]==0
&& i != 1
&& i != 2)
{
[M appendString:@"元整"];
break;
}
[M appendString:MyScale[i-1]];
}
self.label.text = M;
return M;
}
11.通过view获取Controller
- (UIViewController *)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}