创建视图的常用代码
Posted 活最好的自己
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建视图的常用代码相关的知识,希望对你有一定的参考价值。
一,计算 label 宽度
1 - (CGFloat)getCurExpLabelBgWidth:(NSString *)str 2 { 3 str = ISSTRING(str) ? str : @"0"; 4 UILabel *label = [[UILabel alloc] init]; 5 CGFloat fontSize = 12 * kBigScreenViewWidthRate; 6 label.font = [[PTVConfig instance] normalFont:fontSize]; 7 label.text = str; 8 CGRect rect = [label textRectForBounds:CGRectMake(0, 0, MAXFLOAT, fontSize ) limitedToNumberOfLines:1]; 9 CGFloat length = rect.size.width; 10 return length; 11 }
二,判断字符串中是否包含英文字母
1 - (BOOL)isStringContainNumberWith:(NSString *)str { 2 NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil]; 3 NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; 4 //count是str中包含[A-Za-z]数字的个数,只要count>0,说明str中包含数字 5 if (count > 0) { 6 return YES; 7 } 8 return NO; 9 }
三,从横屏跳转到竖屏
1 if (!([PTVConfig instance].orotatemask == UIInterfaceOrientationMaskPortrait)) 2 { 3 4 [PTVConfig instance].orotatemask = UIInterfaceOrientationMaskPortrait; 5 [[PTVConfig instance] setOrientation:UIDeviceOrientationPortrait]; 6 [UIViewController attemptRotationToDeviceOrientation]; 7 } 8 [GETVIEWCONTROLLERMGR pushLoginController:self.parentVc.navigationController];
四,带秒表的定时器
1 - (void)startPkTimer 2 { 3 _percens = 100; 4 [self removeCountTimer]; 5 _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(_timerAction) userInfo:nil repeats:YES]; 6 [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; 7 } 8 9 - (void)_timerAction 10 { 11 _percens -= 5 ; 12 if(_percens <= 0){ 13 _countWownTimer -= 1; 14 _percens = 100; 15 } 16 NSLog(@"lz465350===>%li---%li",(long)_countWownTimer,(long)_percens); 17 if (_countWownTimer <= -1 ) { 18 dispatch_async(dispatch_get_main_queue(), ^{ 19 if (self.timerFinished) 20 { 21 self.timerFinished(); 22 } 23 [self removeCountTimer]; 24 }); 25 return; 26 } else { 27 NSString *text = [self getTimerText:_countWownTimer percens:_percens]; 28 dispatch_async(dispatch_get_main_queue(), ^{ 29 self.text = text; 30 }); 31 } 32 }
以上是关于创建视图的常用代码的主要内容,如果未能解决你的问题,请参考以下文章