iOS小Tips

僅為我自己記錄用的,一些亂七八糟的東西,有需要歡迎使用躬存,覺得小白的大神請路過张惹。。岭洲。

宏定義判斷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];}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末告私,一起剝皮案震驚了整個濱河市屎暇,隨后出現(xiàn)的幾起案子优俘,更是在濱河造成了極大的恐慌夫否,老刑警劉巖悄晃,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谣蠢,死亡現(xiàn)場離奇詭異,居然都是意外死亡淌喻,警方通過查閱死者的電腦和手機姆坚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門笆搓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來屯掖,“玉大人,你說我怎么就攤上這事襟衰√” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵瀑晒,是天一觀的道長绍坝。 經常有香客問我,道長苔悦,這世上最難降的妖魔是什么轩褐? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮玖详,結果婚禮上把介,老公的妹妹穿的比我還像新娘。我一直安慰自己蟋座,他們只是感情好拗踢,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著向臀,像睡著了一般巢墅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天君纫,我揣著相機與錄音驯遇,去河邊找鬼。 笑死蓄髓,一個胖子當著我的面吹牛叉庐,可吹牛的內容都是我干的。 我是一名探鬼主播双吆,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼眨唬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了好乐?” 一聲冷哼從身側響起匾竿,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蔚万,沒想到半個月后岭妖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡反璃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年昵慌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淮蜈。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡斋攀,死狀恐怖,靈堂內的尸體忽然破棺而出梧田,到底是詐尸還是另有隱情淳蔼,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布裁眯,位于F島的核電站鹉梨,受9級特大地震影響,放射性物質發(fā)生泄漏穿稳。R本人自食惡果不足惜存皂,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望逢艘。 院中可真熱鬧旦袋,春花似錦、人聲如沸埋虹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽搔课。三九已至胰柑,卻和暖如春截亦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背柬讨。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工崩瓤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人踩官。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓却桶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蔗牡。 傳聞我的和親對象是個殘疾皇子颖系,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內容

  • 翻譯自“Auto Layout Guide”。 2 自動布局細則手冊 2.1 堆棧視圖 接下來的章節(jié)展示了如何使用...
    lakerszhy閱讀 1,864評論 3 9
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理辩越,服務發(fā)現(xiàn)嘁扼,斷路器,智...
    卡卡羅2017閱讀 134,656評論 18 139
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程黔攒,因...
    小菜c閱讀 6,409評論 0 17
  • 工作了兩年多趁啸,一直有個“壞習慣”,就是將工作中遇到的一些問題督惰、技巧或心得記在印象筆記里面不傅,按理來說,作為一個...
    F森閱讀 2,014評論 3 26
  • 咖啡還是老樣子赏胚。 今天嗓子狀態(tài)還可以访娶,前兩天我剛試著錄一首歌,也不知道是不是因為嗓子的狀態(tài)不太一樣觉阅?反正和以前的錄...
    馮毓閱讀 150評論 0 0