iOS实用方法集锦(二)

UINavigationController Push过去的title不居中

调整的思想是,把前一个UIViewcontroller的返回按钮制空

    NSArray *viewControllerArray = [self.navigationController viewControllers];

    long previousViewControllerIndex = [viewControllerArray indexOfObject:self] - 1;
    UIViewController *previous;

    if (previousViewControllerIndex >= 0) {
        previous = [viewControllerArray objectAtIndex:previousViewControllerIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                     initWithTitle:@""
                                                     style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:nil];
    }

金额计算使用Fload和double精度不精确

使用系统提供的计算金额的方式

- (NSString *)decimalNumberCalucate:(NSString *)originValue1 originValue2:(NSString *)originValue2 calucateWay:(NSInteger)calucateWay
{
       NSDecimalNumberHandler *numUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    NSDecimalNumber *decimalNumber1 = [NSDecimalNumber decimalNumberWithString:originValue1];
    NSDecimalNumber *decimalNumber2 = [NSDecimalNumber decimalNumberWithString:originValue2];
    NSDecimalNumber *product;
    switch (calucateWay) {
        case 1:
            product = [decimalNumber1 decimalNumberByAdding:decimalNumber2 withBehavior:numUp];
            break;

        case 2:
            product = [decimalNumber1 decimalNumberBySubtracting:decimalNumber2 withBehavior:numUp];
            break;

        case 3:
            product = [decimalNumber1 decimalNumberByMultiplyingBy:decimalNumber2 withBehavior:numUp];
            break;

        case 4:
            product = [decimalNumber1 decimalNumberByDividingBy:decimalNumber2 withBehavior:numUp];
            break;

        default:
            break;
    }

    return [product stringValue];
}

数字千分位并保留两位小数

+ (NSString *)stringChangeDecimalStyleWith:(NSString *)string{
    NSNumberFormatter *numformatter = [[NSNumberFormatter alloc] init];
    [numformatter setPositiveFormat:@"###,##0.00;"];
    NSString *str1 = [NSString stringWithFormat:@"%.2f",[string doubleValue]];
    NSString *str2 = [numformatter stringFromNumber:[NSNumber numberWithDouble:[str1 doubleValue]]];
    return str2;
}

获取label中某一段文字的frame(换行的暂未解决)

 NSRange firRange = [xieyiStr rangeOfString:@"要获取的字符串" options:NSCaseInsensitiveSearch];
    NSString *newstr = [@"一个完成的字符串中要获取的字符串所占据区域的frame" substringToIndex:secRange.location];
    CGSize size = [prefix sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    NSLog(@"%f",size.width);
    CGRect newRect = CGRectMake(allLabel.frame.origin.x +size.width, allLabel.frame.origin.y, size.width, allLabel.frame.size.height);

判断点击的点是否在某一个区域(或者在某一个view中)

首先定义一个区域,这个区域可以重其他地方获取

CGPathRef path = CGPathCreateWithRect(CGRectMake(0, 0, 100, 100), NULL);
point的获取方法,可以冲手势获取,也可以重touch获取
point的获取方法:
(1)手势获取
CGPoint point = [Recognizer locationInView:Recognizer.view];
(2)touch获取
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:touch.view];
(1)是否在某个区域
BOOL isInPath = CGPathContainsPoint(path, NULL, point, NO);
(2)是否在某个视图
BOOL isInView = CGRectContainsPoint(view.frame, point);

获取键盘的便宜高度

添加键盘的通知,然后获取高度

- (void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *info = notification.userInfo;
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGFloat height  = keyboardSize.height;
}

切换摄像头

@property (nonatomic ,strong) AVCaptureSession *session;
 NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
    if (cameraCount > 1) {
        NSError *error;
        AVCaptureDeviceInput *newVideoInput;
        AVCaptureDevicePosition position = [[_inPut device] position];

        if (position == AVCaptureDevicePositionBack)
            newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:&error];
        else if (position == AVCaptureDevicePositionFront)
            newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:&error];
        else
            return;

        if (newVideoInput != nil) {
            [self.session beginConfiguration];
            [self.session removeInput:self.inPut];
            if ([self.session canAddInput:newVideoInput]) {
                [self.session addInput:newVideoInput];
                //                [self setVideoInput:newVideoInput];
            } else {
                [self.session addInput:newVideoInput];
            }
            [self.session commitConfiguration];
        } else if (error) {
            NSLog(@"toggle carema failed, error = %@", error);
        }
    }

runtime拦截方法并替换方法

+ (BOOL)resolveInstanceMethod:(SEL)sel{
    if (sel == @selector(dosomething)) {
        NSLog(@"do");
        class_addMethod([self class], sel, (IMP)changeMethodIMP, "v@:");
        class_addMethod([self class], sel, (IMP)newMethod,"i@:@");
        return YES;
    }else{
        return [super resolveInstanceMethod:sel];
    }

}
void changeMethodIMP (id self , SEL _cmd){
    NSLog(@"lod");
}
int newMethod (id self,SEL _cmd ,int str){
    return 100;
}

OC简单调用JS

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    JSContext *cont = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    //具体调用在下面的str中定义
    NSString *str = @"alert ('test alert')";//一个类似uialertcontroller的弹窗
    [cont evaluateScript:str];
}

未完待续,回继续收集并实践

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值