1.获取view所在的controller
- (UIViewController *)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
2.改变图片颜色
UIImageView * praiseImageView = (UIImageView *)tapGesture.view;
UIImage * praiseImage = [[UIImage imageNamed:@"点赞.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
praiseImageView.tintColor = [UIColor colorWithHexString:@"#47a0db" alpha:1.0];
praiseImageView.image = praiseImage;
3.颜色渐变层CAGradientLayer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.bounds;
gradientLayer.colors = @[(__bridge id)[UIColor purpleColor].CGColor,(__bridge id)[UIColor redColor].CGColor];
gradientLayer.startPoint = CGPointMake(0, 0); // x的相对位置,y的相对位置,如果x不设置为0.5,则渐变色会斜着
gradientLayer.endPoint = CGPointMake(1, 1);
gradientLayer.locations = @[@0.5,@1]; // 各颜色的区间位置
4.数字价格格式化
NSNumberFormatter*formatter = [[NSNumberFormatteralloc]init];
formatter.numberStyle=NSNumberFormatterDecimalStyle;
NSString*newAmount = [formatterstringFromNumber:[NSNumbernumberWithint:123456789]];
(注意传入参数的数据长度,还可用double类型等)
NSNumberFormatter类有个属性numberStyle,是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:
enum{
NSNumberFormatterNoStyle =kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle =kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle =kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle =kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle =kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle =kCFNumberFormatterSpellOutStyle
};
各个枚举对应输出数字格式的效果如下:
123456789
123,456,789
¥123,456,789.00
-539,222,988%
1.23456789E8
一亿二千三百四十五万六千七百八十九
5.计算时间间隔的组件
使用NSDateComponents可以很方便的计算时间,比如1小时前,20天前,5分钟前等等
6.解决textField和textView输入文字时实时显示富文本时,文本高亮部分显示错误问题
- (void)textViewDidChange:(UITextView *)textView {
UITextRange *selectedRange = [textView markedTextRange];
// 获取高亮字符的位置
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 没有高亮文字是采取处理富文本
if (!position) { 在这里处理输入的文字,使变成富文本
}
}
7. 去除tableView的separator的设置
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPat{
if([cellrespondsToSelector:@selector(setLayoutMargins:)]) {
[cellsetLayoutMargins:UIEdgeInsetsZero];
}
if([cellrespondsToSelector:@selector(setSeparatorInset:)]){
[cellsetSeparatorInset:UIEdgeInsetsMake(0.0,LabelToXWidth,0.0,0.0)];
}
}
创建tableView时,仍需设置一下内容
if([_tableViewrespondsToSelector:@selector(setSeparatorInset:)]) {
[_tableViewsetSeparatorInset:UIEdgeInsetsMake(0.0,0.0,0.0,0.0)];
}
if([_tableViewrespondsToSelector:@selector(setLayoutMargins:)]) {
[_tableViewsetLayoutMargins:UIEdgeInsetsZero];
}
if([_tableViewrespondsToSelector:@selector(cellLayoutMarginsFollowReadableWidth)]) {
_tableView.cellLayoutMarginsFollowReadableWidth=NO;
}
8. collectionView的获取到的visibleCells未按照indexPath排序问题
- (NSArray *)getSortedVisibleCellFromCollectionView {
NSArray * visibleCellArray = [self.collectionView visibleCells];
NSArray * sortedCellArray = [visibleCellArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath * path1 = [self.collectionView indexPathForCell:obj1];
NSIndexPath * path2 = [self.collectionView indexPathForCell:obj2];
return [path1 compare:path2];
}];
return sortedCellArray;
}