僅為我自己記錄用的,一些亂七八糟的東西,有需要歡迎使用躬存,覺得小白的大神請路過张惹。。岭洲。
宏定義判斷null字符
#define NSStringIsBlank(string) (![string isKindOfClass:[NSString class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""])
#define NSStringIsPresent(string) !NSStringIsBlank(string)
cocoapods
platform :ios, '8.0'
target '項目名稱' do
pod 'AFNetworking' , '~> 3.0'
end
字符串-設置顏色不同
-(NSMutableAttributedString *)changeStrColor:(NSString *)string WithColor:(UIColor *)color WithLocation:(NSUInteger)location andLength:(NSUInteger)length
{ //將string格式變?yōu)閍ttributedString
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:string];
//設置color顏色 loc起始位置 len長度
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)];
return attributedStr;
}
字符串 - 設置字體大小不同
-(NSMutableAttributedString *)changeStrFont:(NSString *)string withLength:(NSString *)length{
NSMutableAttributedString *attributedStr =[[NSMutableAttributedString alloc] initWithString:string];
[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(14, length.length)];
return attributedStr;
}
cell.painFront.attributedText = strFront;
數(shù)組倒序
NSArray *array = (NSMutableArray *)[[array reverseObjectEnumerator] allObjects];
字符串 - 根據(jù)字體大小改變label高度
-(CGFloat)getHeightWithText:(NSString *)text{
CGRect r = [text boundingRectWithSize:CGSizeMake(kScreenSize.width - 45 ,10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil];
return r.size.height;
}
提示框
#define IS_iOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0f
-(void)alertShowWithMsg:(NSString *)msg{
if (IS_iOS8) {
UIAlertController *tipVc = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[tipVc addAction:cancleAction];
[self presentViewController:tipVc animated:YES completion:nil];
}else{
UIAlertView *tip = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[tip show];
}
}
[[UIView appearance] setTintColor:XK_COL_RGB(0x2edead)];//alertView的提示字體顏色
外框
[view.layer setBorderWidth:2.0];
[view.layer setBorderColor:[UIColor clearColor].CGColor];
切圓角
[view.layer setCornerRadius:Radius];
view.layer.masksToBounds = YES;
字符串 - 設置按鈕的下劃線
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"常見問題"];
NSRange strRange = {0,[str length]};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_ProblemButton setAttributedTitle:str forState:UIControlStateNormal];
設置textview的placeholder
-(void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length == 0) {
self.GreatLab.text = @"placeholder"; //自己設置的Label宛逗,用于placeholder
} else {
self.GreatLab.text = @"";
}
}
找底層viewcontroller 父視圖
-(UIViewController *)superViewController{
for (UIView *next = [self superview]; next; next = next.superview){
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
找底層rootViewController 父視圖
UIViewController *vc = [UIApplication sharedApplication].windows[0].rootViewController
撥打電話
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@“,@“電話號碼"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
隱藏電話或銀行卡中間幾位
NSString *tel = [self.attendant.payCard stringByReplacingCharactersInRange:NSMakeRange(4, 12) withString:@"************"];
cell.bankNub.text = tel;
字符串 - 分割字符串
NSString *month = [NSString stringWithFormat:@"%@",self.walletListInfo.month];
NSMutableArray *strArray = [NSMutableArray array];
NSArray *array = [month componentsSeparatedByString:@"-"];
for (int i = 0; i < [array count]; i++) {
NSLog(@"string:%@", [array objectAtIndex:i]);
NSString *str = [array objectAtIndex:i];
[strArray addObject:str];
}
根據(jù)日期查周幾
NSDateComponents *_comps = [[NSDateComponents alloc] init];
[_comps setDay:[day integerValue]];
[_comps setMonth:[mouth integerValue]];
[_comps setYear:[year integerValue]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *_date = [gregorian dateFromComponents:_comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:_date];
int _weekday = (int)[weekdayComponents weekday];
判斷用戶是否允許通知
+(BOOL)isAllowedNotification {
//iOS8 check if user allow notification
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if (sysVer >= 8) {// system is iOS8
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone != setting.types) {
return YES;
}
} else {//iOS7
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone != type)
return YES;
}
return NO;
}
在工程中配置PCH文件,添加 $(TARGET_NAME)/$(TARGET_NAME).pch
添加PCH
刷新tableview的某個區(qū)
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
[set addIndex:3];
[set addIndex:4];
[_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
動畫 - 旋轉
點擊旋轉到上面盾剩,再點擊再旋轉到下方
-(void)transformAnimationWithfrom:(float)from To:(float)To image:(UIImageView *)image {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //默認是順時針效果雷激,若將fromValue和toValue的值互換,則為逆時針效果
animation.fromValue = [NSNumber numberWithFloat:from];
animation.toValue = [NSNumber numberWithFloat:To];
animation.duration = 0.5;
animation.autoreverses = NO;
animation.fillMode =kCAFillModeForwards;
animation.repeatCount = 1;
[image.layer addAnimation:animation forKey:nil];}