一、调节Label的行间距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];
//调整行间距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.contentLabel.text length])];
self.contentLabel.attributedText = attributedString;
二、iOS 开发中一些相关的路径
1.模拟器的位置:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs
2.文档安装位置:
/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
3.插件保存路径:
/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
4.自定义代码段的保存路径:(如果找不到CodeSnippets文件夹,可以自己新建一个CodeSnippets文件夹):
~/Library/Developer/Xcode/UserData/CodeSnippets/
5.证书路径:
~/Library/MobileDevice/Provisioning Profiles
三、获取 iOS 路径的方法
获取home目录路径的函数
NSString *homeDir = NSHomeDirectory();
获取Documents目录路径的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
获取tmp目录路径的方法
NSString *tmpDir = NSTemporaryDirectory();
四、字符串相关操作
- 去除所有的空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""]
- 去除首尾的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- 全部字符转为大写字母
- (NSString *)uppercaseString;
- 全部字符转为小写字母
- (NSString *)lowercaseString
五、控件的局部圆角问题
如果遇到一个设置一个控件(button或者label),只要右边的两个角圆角,或者只要一个圆角。该怎么办呢。这就需要图层蒙版来帮助我们了
CGRect rect = CGRectMake(0, 0, 100, 50);
CGSize radio = CGSizeMake(5, 5);//圆角尺寸
// 可以设置圆角的位置,根据你的需求而定
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
masklayer.frame = button.bounds;
masklayer.path = path.CGPath;//设置路径
button.layer.mask = masklayer;
六、给TableView或者CollectionView的cell添加简单动画
只要在willDisplayCell方法中对将要显示的cell做动画即可:
- UITableViewCell:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *array = tableView.indexPathsForVisibleRows;
NSIndexPath *firstIndexPath = array[0];
//设置anchorPoint
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//为了防止cell视图移动,重新把cell放回原来的位置
cell.layer.position = CGPointMake(0, cell.layer.position.y);
//设置cell 按照z轴旋转90度,注意是弧度
if (firstIndexPath.row < indexPath.row) {
cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0);
}else{
cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:1 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1.0;
}];
}
- UICollectionViewCell:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2 != 0) {
cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0);
}else{
cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:0.7 animations:^{
cell.transform = CGAffineTransformIdentity;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
}];
}
七、给UIView设置图片
UIImage *image = [UIImage imageNamed:@"playing"];
layerView.layer.contents = (__bridge id)image.CGImage;
// 同样可以设置显示的图片范围,不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height
layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5);
八、图片处理只拿到图片的一部分
UIImage *image = [UIImage imageNamed:filename];
CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
//这里的宽高是相对于图片的真实大小
//比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];
九、设置背景半透明的几种方法
UIView *titlesView = [[UIView alloc] init];
// 设置半透明背景色
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
// titlesView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
// titlesView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
// 注意:子控件会继承父控件设置的alpha透明度,如果在titlesView添加一个label的话,label的透明度会跟随titlesView的透明变化
// titlesView.alpha = 0.5;
titlesView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 35);
十、UITableView的Group样式下顶部空白处理
//分组列表头部空白处理
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
self.tableView.tableHeaderView = view;
十一、获取某个view所在的控制器
- (UIViewController *)viewController{
UIViewController *viewController = nil;
UIResponder *next = self.nextResponder;
while (next){
if ([next isKindOfClass:[UIViewController class]]){
viewController = (UIViewController *)next;
break;
}
next = next.nextResponder;
}
return viewController;
}
十二、两种方法删除NSUserDefaults所有记录
- 第一种方法
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults]removePersistentDomainForName:appDomain];
- 第二种方法
- (void)resetDefaults{
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict){
[defs removeObjectForKey:key];
}
[defs synchronize];
}
十三、字符串反转
- 第一种方法
- (NSString *)reverseWordsInString:(NSString *)str{
NSMutableString *newString = [[NSMutableString alloc]initWithCapacity:str.length];
for (NSInteger i = str.length - 1; i >= 0 ; i --){
unichar ch = [str characterAtIndex:i];
[newString appendFormat:@"%c", ch];
}
return newString;
}
- 第二种方法
- (NSString*)reverseWordsInString:(NSString*)str{
NSMutableString *reverString = [NSMutableString stringWithCapacity:str.length];
[str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reverString appendString:substring];
}];
return reverString;
}
十四、获取汉字的拼音
+ (NSString *)transform:(NSString *)chinese{
//将NSString装换成NSMutableString
NSMutableString *pinyin = [chinese mutableCopy];
//将汉字转换为拼音(带音标)
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
NSLog(@"%@", pinyin);
十五、去掉拼音的音标
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
//返回最近结果
return pinyin;
}
十六、判断view是不是指定视图的子视图
BOOL isView = [textView isDescendantOfView:self.view];
十七、NSArray 快速求总和 最大值 最小值 和 平均值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);
十八、修改UITextField中Placeholder的文字颜色
[textField setValue:[UIColor redColor]forKeyPath:@"_placeholderLabel.textColor"];
十九、获取一个类的所有子类
+ (NSArray *) allSubclasses{
Class myClass = [self class];
NSMutableArray *mySubclasses = [NSMutableArray array];
unsigned int numOfClasses;
Class *classes = objc_copyClassList(&numOfClasses);
for (unsigned int ci = 0; ci < numOfClasses; ci++){
Class superClass = classes[ci];
do{
superClass = class_getSuperclass(superClass);
} while (superClass && superClass != myClass);
if (superClass){
[mySubclasses addObject: classes[ci]];
}
}
free(classes);
return mySubclasses;
}
二十、取消UICollectionView的隐式动画
UICollectionView在reloadItems的时候,默认会附加一个隐式的fade动画,有时候很讨厌,尤其是当你的cell是复合cell的情况下(比如cell使用到了UIStackView)。
下面几种方法都可以帮你去除这些动画
方法一
[UIView performWithoutAnimation:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
}];
方法二
[UIView animateWithDuration:0 animations:^{
[collectionView performBatchUpdates:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
} completion:nil];
}];
方法三
[UIView setAnimationsEnabled:NO];
[self.trackPanel performBatchUpdates:^{
[collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
} completion:^(BOOL finished) {
[UIView setAnimationsEnabled:YES];
}];
二十一、CocoaPods pod install/pod update更新慢的问题
pod install --verbose --no-repo-update
pod update --verbose --no-repo-update
如果不加后面的参数,默认会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少
二十二、查找一个视图的所有子视图
- (NSMutableArray *)allSubViewsForView:(UIView *)view{
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
for (UIView *subView in view.subviews){
[array addObject:subView];
if (subView.subviews.count > 0){
[array addObjectsFromArray:[self allSubViewsForView:subView]];
}
}
return array;
}