目录
- 用GCD的方式来实现单例
- TabBar的图片显示原始样貌而非变为默认的蓝色
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
- 设置导航的标题
- 计算时间差
- 原价上的删除线(较为直接粗暴的方法)
- 停靠模式
- 去掉cell间的分隔线
- 快速创建视图控制器
- 使用if判断增强程序的鲁棒性
用GCD的方式来实现单例
+ (DownloadManager *)sharedInstance
{
static DownloadManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[DownloadManager alloc] init];
});
return manager;
}
TabBar的图片显示原始样貌而非变为默认的蓝色- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
ctrl.tabBarItem.image = [[UIImage imageNamed:imageArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
设置导航的标题
self.navigationItem.titleView = <#UIView#>;
计算时间差
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [model.expireDatetime substringToIndex:model.expireDatetime.length - 2];
NSDate *releaseDate = [df dateFromString:dateStr];
NSCalendar *calendar = [NSCalendar currentCalendar];
/*
计算时间差
第一个参数:年月日时分秒
第二个参数:开始时间
第三个参数:结束时间
第四个参数:选项(传0就可以)
*/
unsigned int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponents = [calendar components:unit fromDate:[NSDate date] toDate:releaseDate options:0];
self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", dateComponents.hour, dateComponents.minute, dateComponents.second];
原价上的删除线(较为直接粗暴的方法)
CGFloat width = [priceStr boundingRectWithSize:CGSizeMake(300, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size.width;
// 画横线
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[self.priceLabel addSubview:lineView];
停靠模式
starImageView.contentMode = UIViewContentModeLeft;
starImageView.clipsToBounds = YES;
去掉cell间的分隔线
_tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
快速创建视图控制器
// 视图控制器数组
NSArray *ctrlArray = @[@"LimitFreeViewController", @"ReduceViewController", @"FreeViewController", @"SubjectViewController", @"RankViewController"];
// 循环创建视图控制器
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < ctrlArray.count; i++) {
// 视图控制器
Class cls = NSClassFromString(ctrlArray[i]);
UIViewController *ctrl = [[cls alloc] init];
// 创建导航
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:ctrl];
[array addObject:navCtrl];
}
// 给tabBarCtrl的管理视图控制器属性赋值
self.viewControllers = array;
使用if判断增强程序的鲁棒性
+ (UILabel *)createLabelWithFrame:(CGRect)frame title:(NSString *)title textColor:(UIColor *)color font:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment numberOfLines:(NSInteger)numberOfLines
{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
#warning if
if (title) {
label.text = title;
}
if (color) {
label.textColor = color;
}
…………………………………………
return label;
}