總結(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;
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_1
、animate_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);
}
}
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 手指超出控制范圍的控制中的觸摸事件