iOS小技巧

1,119 阅读2分钟

原文没有排版,看的感觉不舒服,重新排版转发一下了(有部分删减掉了)

一、调用代码使APP进入后台,达到点击Home键的效果。(私有API)

[[UIApplication sharedApplication] performSelector:@selector(suspend)];

suspend的英文意思:悬、挂、暂停

二、获取UIWebView的高度 个人

- (void)webViewDidFinishLoad:(UIWebView *)webView  {    
 CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];      CGRect frame = webView.frame;      webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);  
 }

三、UITableView收起键盘何必这么麻烦 一个属性搞定,效果好(UIScrollView同样可以使用) 以前是不是觉得[self.view endEditing:YES];很屌,这个下面的更屌。

yourTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

四、设置UILable  的行间距  和 计算带行间距的高度

/*

UILabel* 展示的控件

(NSString*)str  内容

withFont:(float)font 字体大小

WithSpace:(float)space 行间距

*/

//给UILabel设置行间距和字间距

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(float)font WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space; //设置行间距

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

//设置字间距 NSKernAttributeName:@1.5f

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];

label.attributedText = attributeStr;

}

/*

计算UILabel的高度(带有行间距的情况)

(NSString*)str  内容

withFont:(float)font 字体大小

WithSpace:(float)space 行间距

(CGFloat)width  UILable的宽度

*/

//计算UILabel的高度(带有行间距的情况)

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(float)font withWidth:(CGFloat)width WithSpace:(float)space{

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];

paraStyle.lineBreakMode = NSLineBreakByCharWrapping;

paraStyle.alignment = NSTextAlignmentLeft;

paraStyle.lineSpacing = space;

paraStyle.hyphenationFactor = 1.0;

paraStyle.firstLineHeadIndent = 0.0;

paraStyle.paragraphSpacingBefore = 0.0;

paraStyle.headIndent = 0;

paraStyle.tailIndent = 0;

UIFont *tfont = [UIFont systemFontOfSize:font];

NSDictionary *dic = @{NSFontAttributeName:tfont, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f

};

CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

return size.height;

}

五、 禁止程序运行时自动锁屏

[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

六、CocoaPods pod install/pod update更新慢的问题

pod install –verbose –no-repo-update 
pod update –verbose –no-repo-update 

如果不加后面的参数,默认会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。

七、修改textFieldplaceholder字体颜色和大小

textField.placeholder = @"请输入用户名";  
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

八、禁止textField和textView的复制粘贴菜单

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{    
	if ([UIMenuController sharedMenuController]) {      
		[UIMenuController sharedMenuController].menuVisible = NO;  
 		}    
 	return NO;
  }

九、级三级页面隐藏系统tabbar 1、单个处理

YourViewController *yourVC = [YourViewController new];
yourVC.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:yourVC animated:YES];

十、取消系统的返回手势

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

十一、添加pch文件的步聚

1:创建新文件 ios->other->PCH file,创建一个pch文件:“工程名-Prefix.pch”:

2:将building setting中的precompile header选项的路径添加“$(SRCROOT)/项目名称/pch文件名”(例如:$(SRCROOT)/LotteryFive/LotteryFive-Prefix.pch)

3:将Precompile Prefix Header为YES,预编译后的pch文件会被缓存起来,可以提高编译速度