总结一下常见的小问题.
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之间增加间距
方法一,每个分区只显示一行cell,分区头当作你想要的间距(注意,从数据源数组中取值的时候需要用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上原本的内容放在你的view上,而不是contentView
上,这样能伪造出一个间距来。
方法三,自定义cell,重写setFrame:
方法
- (void)setFrame:(CGRect)frame{
frame.size.height -= 20;
[super setFrame:frame];
}
6. 播放一张张连续的图片
加入现在有三张图片分别为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. 查看系统所有字体
for (id familyName in [UIFont familyNames]) {
NSLog(@"%@", familyName);
for (id fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"---%@---", fontName);
}
}
9. 判断一个字符串是否为数字
//判断一个字符串是否为数字
NSString *str = @"452436535436";
NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([str rangeOfCharacterFromSet:notDigits].location == NSNotFound) {
NSLog(@"是数字");
}else{
NSLog(@"不是数字");
}
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>";
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];
}
//结束动画
- (void)stopAnimate {
[UIView animateWithDuration:0.25 delay:0.0 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear) animations:^{
self.backView.transform = CGAffineTransformIdentity;
} completion:nil];
}
17. 通知监听App生命周期
UIApplicationDidEnterBackgroundNotification 应用程序进入后台
UIApplicationWillEnterForegroundNotification 应用程序将要进入前台
UIApplicationDidFinishLaunchingNotification 应用程序完成启动
UIApplicationDidFinishLaunchingNotification 应用程序由挂起变的活跃
UIApplicationWillResignActiveNotification 应用程序挂起(有电话进来或者锁屏)
UIApplicationDidReceiveMemoryWarningNotification 应用程序收到内存警告
UIApplicationDidReceiveMemoryWarningNotification 应用程序终止(后台杀死、手机关机等)
UIApplicationSignificantTimeChangeNotification 当有重大时间改变(凌晨0点,设备时间被修改,时区改变等)
UIApplicationWillChangeStatusBarOrientationNotification 设备方向将要改变
UIApplicationDidChangeStatusBarOrientationNotification 设备方向改变
UIApplicationWillChangeStatusBarFrameNotification 设备状态栏frame将要改变
UIApplicationDidChangeStatusBarFrameNotification 设备状态栏frame改变
UIApplicationBackgroundRefreshStatusDidChangeNotification 应用程序在后台下载内容的状态发生变化
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件被锁定,无法访问
UIApplicationProtectedDataWillBecomeUnavailable 本地受保护的文件可用了
18. 触摸事件类型
UIControlEventTouchCancel 取消控件当前触发的事件
UIControlEventTouchDown 点按下去的事件
UIControlEventTouchDownRepeat 重复的触动事件
UIControlEventTouchDragEnter 手指被拖动到控件的边界的事件
UIControlEventTouchDragExit 一个手指从控件内拖到外界的事件
UIControlEventTouchDragInside 手指在控件的边界内拖动的事件
UIControlEventTouchDragOutside 手指在控件边界之外被拖动的事件
UIControlEventTouchUpInside 手指处于控制范围内的触摸事件
UIControlEventTouchUpOutside 手指超出控制范围的控制中的触摸事件