美文网首页
iOS 基础工具类总结

iOS 基础工具类总结

作者: 多肉肉 | 来源:发表于2020-01-13 10:34 被阅读0次

UITableView的Group样式下顶部空白处理

//分组列表头部空白处理

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];

self.tableView.tableHeaderView = view;

UITableView的plain样式下,取消区头停滞效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGFloat sectionHeaderHeight = sectionHead.height;

if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView;.contentOffset.y>=0)

{

scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

}

else if(scrollView.contentOffset.y>=sectionHeaderHeight)

{

scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);

}

}

获取某个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];

}

打印系统所有已注册的字体名称

#pragma mark - 打印系统所有已注册的字体名称

void enumerateFonts()

{

for(NSString *familyName in [UIFont familyNames])

{

NSLog(@"%@",familyName);

NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];

for(NSString *fontName in fontNames)

{

NSLog(@"\t|- %@",fontName);

}

}

}

取图片某一像素点的颜色 在UIImage的分类中

- (UIColor *)colorAtPixel:(CGPoint)point

{

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point))

{

return nil;

}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

int bytesPerPixel = 4;

int bytesPerRow = bytesPerPixel * 1;

NSUInteger bitsPerComponent = 8;

unsigned char pixelData[4] = {0, 0, 0, 0};

CGContextRef context = CGBitmapContextCreate(pixelData, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGColorSpaceRelease(colorSpace);

CGContextSetBlendMode(context, kCGBlendModeCopy);

CGContextTranslateCTM(context, -point.x, point.y - self.size.height);

CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), self.CGImage);

CGContextRelease(context);

CGFloat red   = (CGFloat)pixelData[0] / 255.0f;

CGFloat green = (CGFloat)pixelData[1] / 255.0f;

CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;

CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;

return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

}

字符串反转

第一种:

- (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;

}

禁止锁屏,默认情况下,当设备一段时间没有触控动作时,iOS会锁住屏幕。但有一些应用是不需要锁屏的,比如视频播放器。

[UIApplication sharedApplication].idleTimerDisabled = YES;

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

模态推出透明界面

UIViewController *vc = [[UIViewController alloc] init];

UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:vc];

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

{

na.modalPresentationStyle = UIModalPresentationOverCurrentContext;

}

else

{

self.modalPresentationStyle=UIModalPresentationCurrentContext;

}

[self presentViewController:na animated:YES completion:nil];

Xcode调试不显示内存占用

EditScheme  里面有个选项叫叫做enable zoombie Objects  取消选中

显示隐藏文件

//显示

defaults write com.apple.finder AppleShowAllFiles -bool true

killall Finder

//隐藏

defaults write com.apple.finder AppleShowAllFiles -bool false

killall Finder

相关文章

网友评论

      本文标题:iOS 基础工具类总结

      本文链接:https://www.haomeiwen.com/subject/lpegactx.html