計算某個文件\文件夾的大小
@implementation NSString (CLExtension)
//- (unsigned long long)fileSize
//{
// // 總大小
// unsigned long long size = 0;
//
// // 文件管理者
// NSFileManager *mgr = [NSFileManager defaultManager];
//
// // 文件屬性
// NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil];
//
// if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夾
// // 獲得文件夾的大小 == 獲得文件夾中所有文件的總大小
// NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
// for (NSString *subpath in enumerator) {
// // 全路徑
// NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// // 累加文件大小
// size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
// }
// } else { // 文件
// size = attrs.fileSize;
// }
//
// return size;
//}
- (unsigned long long)fileSize
{
// 總大小
unsigned long long size = 0;
// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 是否為文件夾
BOOL isDirectory = NO;
// 路徑是否存在
BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
if (!exists) return size;
if (isDirectory) { // 文件夾
// 獲得文件夾的大小 == 獲得文件夾中所有文件的總大小
NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
for (NSString *subpath in enumerator) {
// 全路徑
NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
}
} else { // 文件
size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
}
return size;
}
@end
CLLog(@"%zd", @"/Users/jimmyC/Desktop".fileSize);
計算文字的寬度
CGFloat titleW = [字符串 sizeWithFont:字體大小].width;
CGFloat titleW = [字符串 sizeWithAttributes:@{NSFontAttributeName : 字體大小}].width;
有透明度的顏色
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2];
[UIColor colorWithWhite:1.0 alpha:0.2];
[[UIColor whiteColor] colorWithAlphaComponent:0.2];
viewWithTag:內部的大致實現思路
@implementation UIView
- (UIView *)viewWithTag:(NSInteger)tag
{
if (self.tag == tag) return self;
for (UIView *subview in self.subviews) {
return [subview viewWithTag:tag];
}
}
@end
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。