iOS 平时常用的一些小方法
平时有一些常用的方法,我就写在这了,方便以后用
● //下划线 //调用 : label.attributedText = [self underLine:string];
- (NSAttributedString *)underLine:(NSString *)str{ NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:str]; NSNumber *number = [NSNumber numberWithInteger:NSUnderlineStyleSingle]; [string addAttribute:NSUnderlineStyleAttributeName value:number range:NSMakeRange(0, [string length])]; return string; }
- //删除线 //调用: label.attributedText = [self strikethroughLine:string];
- (NSAttributedString *)strikethroughLine:(NSString *)str{ NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:str]; NSNumber *number = [NSNumber numberWithInteger:NSUnderlineStyleSingle]; [string addAttribute:NSStrikethroughStyleAttributeName value:number range:NSMakeRange(0, [string length])]; return string; }
- //弹出定时提示框 //调用: [self showAlert:@"请先登录!"];
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框 { UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo]; [promptAlert dismissWithClickedButtonIndex:0 animated:NO]; promptAlert = NULL; } - (void)showAlert:(NSString *) _message{//时间 UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFireMethod:) userInfo:promptAlert repeats:YES]; [promptAlert show]; }
- //网络请求时转类型
//string转JSON - (NSString *)jsonStringFromObject:(id)object { NSError *erro = nil; NSData *jsondata = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&erro]; NSString *jsonString = [[NSString alloc]initWithData:jsondata encoding:NSUTF8StringEncoding]; return jsonString; } //JSON转string - (id)jsonStringToObject:(NSString *)jsonString { NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; return object; }
- //自定义返回按钮
- (void)setupBackItem { self.navigationItem.hidesBackButton = YES; [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; back.frame = CGRectMake(15, 5, 30, 30); [back setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; //有时候系统的导航栏会抽,按钮靠死了视图变,就加上下面注释的代码 //UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn]; //UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; //negativeSpacer.width = 10; //self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:back]; self.navigationItem.leftBarButtonItem = backItem; self.navigationController.navigationBar.barTintColor = MainColor; } - (void)back { [self.navigationController popViewControllerAnimated:YES]; }

浙公网安备 33010602011771号