iOS常用代碼塊demo1

1.GCD定時器


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);

dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒執(zhí)行

dispatch_source_set_event_handler(timer, ^{

//倒計時結(jié)束,關(guān)閉

dispatch_source_cancel(timer);

dispatch_async(dispatch_get_main_queue(), ^{

});

});

dispatch_resume(timer);

2.圖片上繪制文字

- (UIImage *)imageWithTitle:(NSString *)title fontSize:(CGFloat)fontSize

{

//畫布大小

CGSize size=CGSizeMake(self.size.width,self.size.height);

//創(chuàng)建一個基于位圖的上下文

UIGraphicsBeginImageContextWithOptions(size,NO,0.0);//opaque:NO? scale:0.0

[self drawAtPoint:CGPointMake(0.0,0.0)];

//文字居中顯示在畫布上

NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中

//計算文字所占的size,文字居中顯示在畫布上

CGSize sizeText=[title boundingRectWithSize:self.size options:NSStringDrawingUsesLineFragmentOrigin

attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}context:nil].size;

CGFloat width = self.size.width;

CGFloat height = self.size.height;

CGRect rect = CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);

//繪制文字

[title drawInRect:rect withAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[ UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle}];

//返回繪制的新圖形

UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

3.查找一個視圖的所有子視圖

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

}

4.計算文件大小

//文件大小

- (long long)fileSizeAtPath:(NSString *)path

{

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:path])

{

long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;

return size;

}

return 0;

}

//文件夾大小

- (long long)folderSizeAtPath:(NSString *)path

{

NSFileManager *fileManager = [NSFileManager defaultManager];

long long folderSize = 0;

if ([fileManager fileExistsAtPath:path])

{

NSArray *childerFiles = [fileManager subpathsAtPath:path];

for (NSString *fileName in childerFiles)

{

NSString *fileAbsolutePath = [path stringByAppendingPathComponent:fileName];

if ([fileManager fileExistsAtPath:fileAbsolutePath])

{

long long size = [fileManager attributesOfItemAtPath:fileAbsolutePath error:nil].fileSize;

folderSize += size;

}

}

}

return folderSize;

}

5.UIView的設(shè)置部分圓角

CGRect rect = view.bounds;

CGSize radio = CGSizeMake(30, 30);//圓角尺寸

UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這只圓角位置

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];

CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//創(chuàng)建shapelayer

masklayer.frame = view.bounds;

masklayer.path = path.CGPath;//設(shè)置路徑

view.layer.mask = mask layer;


6.計算字符串字符長度,一個漢字算兩個字符

//方法一:

- (int)convertToInt:(NSString*)strtemp

{

int strlength = 0;

char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];

for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++)

{

if (*p)

{

p++;

strlength++;

}

else

{

p++;

}

}

return strlength;

}

//方法二:

-(NSUInteger) unicodeLengthOfString: (NSString *) text

{

NSUInteger asciiLength = 0;

for (NSUInteger i = 0; i < text.length; i++)

{

unichar uc = [text characterAtIndex: i];

asciiLength += isascii(uc) ? 1 : 2;

}

return asciiLength;

}

防止?jié)L動視圖手勢覆蓋側(cè)滑手勢

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];

去掉導(dǎo)航欄返回的標(biāo)題

[[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容