iOS 開發(fā)總結(jié)(三)

總結(jié)一下常見的小問題.

1. 設置UILabel的行間距

  //設置圖片行間距
  NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:label.text];
  NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  style.lineSpacing = 10;
  [attribute addAttribute:NSParagraphStyleAttributeName value:style   range:NSMakeRange(0, label.text.length)];
  label.attributedText = attribute;
設置label間距

2.UILabel顯示不同顏色字體

  //UILabel顯示不同字體顏色
  NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:label.text];
  [attribute setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(0, 5)];
  [attribute setAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} range:NSMakeRange(5, 3)];
  [attribute setAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} range:NSMakeRange(8, 5)];
  label.attributedText = attribute;

3. 比較兩個CGRect/CGSize/CGPoint是否相等

  //比較兩個CGRect/CGSize/CGPoint是否相等
  CGRect rect1 = CGRectMake(0, 0, 30, 40);
  CGRect rect2 = CGRectMake(0, 0, 20, 30);
  //CGSizeEqualToSize(<#CGSize size1#>, <#CGSize size2#>);
  //CGPointEqualToPoint(<#CGPoint point1#>, <#CGPoint point2#>)
  if (CGRectEqualToRect(rect1, rect2)) {
      NSLog(@"相等");
  }else{
      NSLog(@"不相等");
  }

4. 比較兩個NSDate 相差多少小時

  //判斷兩個NSDate相差多少小時
  NSDate *date1 = someDate;
  NSDate *date2 = someOtherDate;
  NSTimeInterval timeInterval = [date1 timeIntervalSinceDate:date2];

5. 每個cell之間增加間距

方法一,每個分區(qū)只顯示一行cell,分區(qū)頭當作你想要的間距(注意理郑,從數(shù)據(jù)源數(shù)組中取值的時候需要用indexPath.section而不是indexPath.row)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    
    return yourArry.count;
  }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    
    return cellSpacingHeight;
}

方法二,在cell的contentView上加個稍微低一點的view涝动,cell上原本的內(nèi)容放在你的view上田度,而不是contentView上,這樣能偽造出一個間距來佩厚。
方法三豺瘤,自定義cell吆倦,重寫setFrame:方法

- (void)setFrame:(CGRect)frame{    
    frame.size.height -= 20;   
    [super setFrame:frame];
}

6. 播放一張張連續(xù)的圖片

加入現(xiàn)在有三張圖片分別為animate_1animate_2坐求、animate_3
// 方法一

  UIImageView *imageView;
  imageView.animationImages = @[[UIImage imageNamed:@"animate_1"],
                                [UIImage imageNamed:@"animate_2"],
                                [UIImage imageNamed:@"animate_3"]];
  imageView.animationDuration = 1.0;

// 方法二

  imageView.image = [UIImage animatedImageNamed:@"animate_" duration:1.0];

方法二解釋下蚕泽,這個方法會加載animate_為前綴的,后邊0-1024瞻赶,也就是animate_0赛糟、animate_1一直到animate_1024

7. 加載gif圖片

推薦使用這個框架 FLAnimatedImage

8. 查看系統(tǒng)所有字體

  for (id familyName in [UIFont familyNames]) {
      NSLog(@"%@", familyName);
      for (id fontName in [UIFont fontNamesForFamilyName:familyName]) {
          NSLog(@"---%@---", fontName);
      }
  }
運行結(jié)果

9. 判斷一個字符串是否為數(shù)字

//判斷一個字符串是否為數(shù)字
  NSString *str = @"452436535436";
  NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
  if ([str rangeOfCharacterFromSet:notDigits].location == NSNotFound) {
      NSLog(@"是數(shù)字");
  }else{
      NSLog(@"不是數(shù)字");
  }

10. 讓一個view在父視圖的中心

  //讓一個view在父視圖的中心
  child.center = [parent convertPoint:parent.center fromView:parent.superview];

11. 保存圖片

  • 保存圖片到本地
  //將圖片保存到本地
  UIImage *image = [UIImage imageNamed:@"aa.jpeg"];
  NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"image.jpeg"];
  [UIImageJPEGRepresentation(image, 1) writeToFile:path atomically:YES];
  • 保存圖片到相冊
  //將圖片保存到相冊
    /**
      *  將圖片保存到iPhone本地相冊
      *  UIImage *image            圖片對象
      *  id completionTarget       響應方法對象
      *  SEL completionSelector    方法
      *  void *contextInfo
    */
  UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error == nil) {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手機相冊" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
      [alert show];
    }else{  
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失敗" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
      [alert show];
    }  
}

12. 判斷一個view是否是另一個view的子視圖

  //判斷一個view是否是另一個view的子視圖
  UIView *view1;
  UIView *view2;
  BOOL isSon = [view1 isDescendantOfView:view2];

13. 導航控制器pop到指定viewController

  //導航控制器指定pop到指定的viewcontroller
  for (UIViewController *vc in self.navigationController.viewControllers) {
      if ([[vc isKindOfClass:[RequireViewController Class]]) {
          [self.navigationController popToViewController:vc animated:YES];
      }
  }

14. UITextView中顯示html

  //UITextView中顯示html
  UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, 200, 150)];
  [self.view addSubview:textView];
  NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>![](http://upload-images.jianshu.io/upload_images/2370110-11fa1d3a2af409dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)";   
  NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType} documentAttributes:nil error:nil];
  textView.attributedText = attribute;

15. 隱藏UITextView/UITextField光標

  textField.tintColor = [UIColor clearColor];

16. 仿蘋果??抖動動畫

  self.backView = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 50, 50)];
  self.backView.backgroundColor = [UIColor redColor];
  [self.view addSubview:self.backView];
  [self startAnimate];
  //    [self performSelector:@selector(stopAnimate) withObject:nil afterDelay:5];
//開始動畫
- (void)startAnimate {
    self.backView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5));
    [UIView animateWithDuration:0.25 delay:0.0 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^ {
    self.backView.transform =      CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5));
  } completion:nil];
}

//結(jié)束動畫
- (void)stopAnimate {
    [UIView animateWithDuration:0.25 delay:0.0 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear) animations:^{
        self.backView.transform = CGAffineTransformIdentity;
    } completion:nil];
}

17. 通知監(jiān)聽App生命周期

UIApplicationDidEnterBackgroundNotification 應用程序進入后臺
UIApplicationWillEnterForegroundNotification 應用程序?qū)⒁M入前臺
UIApplicationDidFinishLaunchingNotification 應用程序完成啟動
UIApplicationDidFinishLaunchingNotification 應用程序由掛起變的活躍
UIApplicationWillResignActiveNotification 應用程序掛起(有電話進來或者鎖屏)
UIApplicationDidReceiveMemoryWarningNotification 應用程序收到內(nèi)存警告
UIApplicationDidReceiveMemoryWarningNotification 應用程序終止(后臺殺死、手機關機等)
UIApplicationSignificantTimeChangeNotification 當有重大時間改變(凌晨0點砸逊,設備時間被修改璧南,時區(qū)改變等)
UIApplicationWillChangeStatusBarOrientationNotification 設備方向?qū)⒁淖?br> UIApplicationDidChangeStatusBarOrientationNotification 設備方向改變
UIApplicationWillChangeStatusBarFrameNotification 設備狀態(tài)欄frame將要改變
UIApplicationDidChangeStatusBarFrameNotification 設備狀態(tài)欄frame改變
UIApplicationBackgroundRefreshStatusDidChangeNotification 應用程序在后臺下載內(nèi)容的狀態(tài)發(fā)生變化
UIApplicationProtectedDataWillBecomeUnavailable 本地受保護的文件被鎖定,無法訪問
UIApplicationProtectedDataWillBecomeUnavailable 本地受保護的文件可用了

18. 觸摸事件類型

UIControlEventTouchCancel 取消控件當前觸發(fā)的事件
UIControlEventTouchDown 點按下去的事件
UIControlEventTouchDownRepeat 重復的觸動事件
UIControlEventTouchDragEnter 手指被拖動到控件的邊界的事件
UIControlEventTouchDragExit 一個手指從控件內(nèi)拖到外界的事件
UIControlEventTouchDragInside 手指在控件的邊界內(nèi)拖動的事件
UIControlEventTouchDragOutside 手指在控件邊界之外被拖動的事件
UIControlEventTouchUpInside 手指處于控制范圍內(nèi)的觸摸事件
UIControlEventTouchUpOutside 手指超出控制范圍的控制中的觸摸事件

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市师逸,隨后出現(xiàn)的幾起案子司倚,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件动知,死亡現(xiàn)場離奇詭異皿伺,居然都是意外死亡,警方通過查閱死者的電腦和手機盒粮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門鸵鸥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人丹皱,你說我怎么就攤上這事妒穴。” “怎么了摊崭?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵讼油,是天一觀的道長。 經(jīng)常有香客問我呢簸,道長矮台,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任根时,我火速辦了婚禮瘦赫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蛤迎。我一直安慰自己耸彪,他們只是感情好,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布忘苛。 她就那樣靜靜地躺著,像睡著了一般唱较。 火紅的嫁衣襯著肌膚如雪扎唾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天南缓,我揣著相機與錄音胸遇,去河邊找鬼。 笑死汉形,一個胖子當著我的面吹牛纸镊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播概疆,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼逗威,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了岔冀?” 一聲冷哼從身側(cè)響起凯旭,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后罐呼,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鞠柄,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年嫉柴,在試婚紗的時候發(fā)現(xiàn)自己被綠了厌杜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡计螺,死狀恐怖夯尽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情危尿,我是刑警寧澤呐萌,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站谊娇,受9級特大地震影響肺孤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜济欢,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一赠堵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧法褥,春花似錦茫叭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至杀饵,卻和暖如春莽囤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背切距。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工朽缎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谜悟。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓话肖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親葡幸。 傳聞我的和親對象是個殘疾皇子最筒,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容