iOS开发常用技能

我把我觉得很好用的方法技能无私奉献一下吧。欢迎收藏关注。

1.删除某个view上面的所有视图

[self.jggView.subviewsmakeObjectsPerformSelector:@selector(removeFromSuperview)];

2.计算label行间距和字间距

#define UILABEL_LINE_SPACE6

#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height

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

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {

NSMutableParagraphStyle*paraStyle = [[NSMutableParagraphStylealloc]init];

paraStyle.lineBreakMode=NSLineBreakByCharWrapping;

paraStyle.alignment=NSTextAlignmentLeft;

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

paraStyle.hyphenationFactor=1.0;

paraStyle.firstLineHeadIndent=0.0;

paraStyle.paragraphSpacingBefore=0.0;

paraStyle.headIndent=0;

paraStyle.tailIndent=0;

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

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

};

NSAttributedString*attributeStr = [[NSAttributedStringalloc]initWithString:strattributes:dic];

label.attributedText= attributeStr;

}

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

-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {

NSMutableParagraphStyle*paraStyle = [[NSMutableParagraphStylealloc]init];

paraStyle.lineBreakMode=NSLineBreakByCharWrapping;

paraStyle.alignment=NSTextAlignmentLeft;

paraStyle.lineSpacing=UILABEL_LINE_SPACE;

paraStyle.hyphenationFactor=1.0;

paraStyle.firstLineHeadIndent=0.0;

paraStyle.paragraphSpacingBefore=0.0;

paraStyle.headIndent=0;

paraStyle.tailIndent=0;

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

};

CGSizesize = [strboundingRectWithSize:CGSizeMake(width,HEIGHT)options:NSStringDrawingUsesLineFragmentOriginattributes:diccontext:nil].size;

returnsize.height;

}

3.更新约束问题的动态计算cell的高度

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath

{

TCell*cell= [TCellshareInstance];

[cellsetNeedsUpdateConstraints];

[cellupdateConstraintsIfNeeded];

[cellsetCellContent:self.data[indexPath.row]];

[cellsetNeedsLayout];

[celllayoutIfNeeded];

CGFloatheight = [cell.contentViewsystemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

returnheight;

}

4.定义此两个宏可以有效的使用masonry

#define MAS_SHORTHAND

#define MAS_SHORTHAND_GLOBALS

5.第三方类库(有机会下载几个研究一下也是不错的)

http://www.th7.cn/Program/IOS/201601/757661.shtml

6.动画详细介绍

http://mp.weixin.qq.com/s?__biz=MzIwOTQ3NzU0Mw==&mid=2247483734&idx=1&sn=33a17000f8fba3db79d8e94046915d09#rd

7.textView添加监听字数

if ((textView.text.length - range.length + text.length) > MAX_INPUT_LENGTH)

{

NSString *substring = [text substringToIndex:MAX_INPUT_LENGTH - (textView.text.length - range.length)];

NSMutableString *lastString = [textView.text mutableCopy];

[lastString replaceCharactersInRange:range withString:substring];

textView.text = [lastString copy];

return NO;

}

else

{

return YES;

}

8.React-Native框架学习资料

React-Native学习指南

本指南汇集React-Native各类学习资源,给大家提供便利。指南正在不断的更新,大家有好的资源欢迎Pull Requests!

同时还有Awesome React-Native系列

https://github.com/jondot/awesome-react-native

教程

react-native 官方api文档http://facebook.github.io/react-native/docs/getting-started.html

react-native 中文api文档 (翻译中)https://github.com/ecomfe/react-native-cn

react.js中文文档http://reactjs.cn/

react.js入门教程(gitbook)http://hulufei.gitbooks.io/react-tutorial/content/introduction.html

react.js快速入门教程 - 阮一峰http://www.ruanyifeng.com/blog/2015/03/react.html

react.js视频教程http://react.nodejs-china.org/t/reactjszhong-wen-shi-pin-jiao-cheng-bai-du-wang-pan/584

react-native第一课http://html-js.com/article/2783

深入浅出 React Native:使用 JavaScript 构建原生应用http://zhuanlan.zhihu.com/FrontendMagazine/19996445

React Native通信机制详解http://blog.cnbang.net/tech/2698/

React Native布局篇http://segmentfault.com/a/1190000002658374

React Native 基础练习指北(一)http://segmentfault.com/a/1190000002645929

React Native 基础练习指北(二)http://segmentfault.com/a/1190000002647733

构建一个简单的列表页和2页导航http://www.xn--cnq920ntha.cn/archives/235#6838470-tsina-1-17436-6a377b1a66595f9ede646cf5c012734c

Diary of Building an iOS App with React Nativehttp://herman.asia/building-a-flashcard-app-with-react-native

Use React Native in Existing iOS Apphttp://blog-en.leapoahead.com/post/use-react-native-in-existing-ios-app

tcomb-form-native使用视频教程(需翻墙)http://react.rocks/example/tcomb-form-native

开源APP

研究源码也是一个很好的学习方式

官方演示Apphttps://github.com/facebook/react-native/tree/master/Examples

ReactNativeRubyChinahttps://github.com/henter/ReactNativeRubyChina

HackerNews-React-Nativehttps://github.com/iSimar/HackerNews-React-Native

React-Native新闻客户端https://github.com/tabalt/ReactNativeNews

newswatch(新闻客户端)https://github.com/bradoyler/newswatch-react-native

buyscreen(购买页面)https://github.com/appintheair/react-native-buyscreen

V2EX客户端https://github.com/samuel1112/v2er

react-native-todohttps://github.com/joemaddalone/react-native-todo

react-native-beerhttps://github.com/muratsu/react-native-beer

react-native-starshttps://github.com/86/react-native-stars

模仿天猫首页的apphttps://github.com/baofen14787/react-native-demo

ReactNativeChesshttps://github.com/csarsam/ReactNativeChess

react native 编写的音乐软件https://github.com/Johnqing/miumiu

react-native-pokedexhttps://github.com/ababol/react-native-pokedex

CNode-React-Nativehttps://github.com/SFantasy/CNode-React-Native

8tracks电台客户端https://github.com/voronianski/EightTracksReactNative

React-Native实现的计算器https://github.com/yoxisem544/Calculator-using-React-Native

房产搜索apphttps://github.com/jawee/react-native-PropertyFinder

知乎专栏apphttps://github.com/LeezQ/react-native-zhihu-app

ForeignExchangeApphttps://github.com/peralmq/ForeignExchangeApp

工具

react-native-snippets(代码提示)https://github.com/Shrugs/react-native-snippets

react-native-babel(使用ES6+)https://github.com/roman01la/react-native-babel

资源网站

React-native官网http://facebook.github.io/react-native/

React-China社区http://react-china.org/

React-native组件库(比较全的组件库)http://react.parts/

React Native Moduleshttp://reactnativemodules.com/

11款React Native开源移动 UI 组件http://www.oschina.net/news/61214/11-react-native-ui-components

业界讨论

谈谈 React Native - 唐巧http://blog.devtang.com/blog/2015/02/01/talk-about-react-native/

如何评价React-Native?http://www.zhihu.com/question/27852694/answer/43990708

React Native概述:背景、规划和风险http://div.io/topic/938

指南:https://github.com/ele828/react-native-guide

9.cocopods文件添加到整个工程以及一些可用的注意事项

1.使用CocoaPods导入头文件不能提示?

在BuildSettings中搜索Header Search Path,然后设置$(PODS_ROOT),并勾选recursive支持递归搜索。

2.代码片段路径:~/Library/Developer/Xcode/UserData/CodeSnippets

1.Too many arguments to function call, expected 0, have 2

- Project - Build Settings - ENABLE_STRICT_OBJC_MSGSEND将其设置为NO

2.导入pch文件

(1)Building Setting中搜索pref

(2)将Prefix Header的值设为$(SRCROOT)/$(PRODUCT_NAME)/$(PRODUCT_NAME).pch

3.sizeClass经验总结:

(1)所有公用的控件要出现在**

(2)所有公共的约束需要出现在**

(2)所有的大块view,如果内部子控件的约束是固定的,约束需要写在**中。

总结:在**中要指定大小,同时一般不指定位置,你可以手动拖动位置,便于查看,在其他详细界面再正确调整,这样多个sizeclass查看界面就不会显得很乱。

10.离线缓存

基于NSURLSession+NSURLProtol的UIWebView离线缓存 主要是使用苹果的黑魔法类:NSURLProtocol来对网络请求进行拦截,拦截后使用自定义的网络去加载数据后进行离线缓存。这样保证在没有网络的情况下,也能保证离线能对之前webView进行浏览

11.button 添加双击事件 并且取消单击事件

单击事件:

-(void)singleClick:(UIButton *)button forEvent:(UIEvent *)event{

[self performSelector:@selector(tabButtonTap:) withObject:button afterDelay:0.2];

}

- (void)tabButtonTap:(UIButton *)sender {

NSLog(@"单击操作");

}

双击事件:

-(void)doubleClick:(UIButton *)button forEvent:(UIEvent *)event

{

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabButtonTap:) object:button];

NSLog(@"双击操作");

}

这个方法

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabButtonTap:) object:button];

解释是Cancels perform requests previously registered with performSelector:withObject:afterDelay:

就是把performSelector:withObject:afterDelay:以前注册的请求取消掉,从代码上看就是取消了tabButtonTap:方法。

但是只能在一个运行循环范围内有效。所以通过这样子就把单击的事件取消了,在后面就可以实现双击的操作,而不必担心会先触发单击。

12.定制tableView的分割线

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath

{

UIEdgeInsetsinsets =UIEdgeInsetsMake(10,10,10,30);

//三个方法并用,实现自定义分割线效果

if([cellrespondsToSelector:@selector(setSeparatorInset:)]) {

cell.separatorInset= insets;

}

if([cellrespondsToSelector:@selector(setLayoutMargins:)]) {

[cellsetLayoutMargins:insets];

}

if([cellrespondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){

[cellsetPreservesSuperviewLayoutMargins:NO];

}

}

实现如下:

UITableView中将分割线样式改为None

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

自定义UITableViewCell中复写- (void)drawRect:(CGRect)rect方法

- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); //上分割线, CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"ffffff"].CGColor); CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); //下分割线 CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e2e2e2"].CGColor); CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1)); }

13.保存网络加载的启动图片。


NSDictionary*responseDic = (NSDictionary*)responseObject;

NSDictionary*dic = [responseDicobjectForKey:@"data"];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

if(dic) {

NSString*picUrl = [dicobjectForKey:@"pic_url"];

if([picUrlisEqualToString:@""]) {

NSLog(@"pic_url:%@",picUrl);

}else{

NSData*data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:picUrl]];

UIImage*image = [UIImageimageWithData:data];

NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString*filePath = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"loading.png"]];//保存文件的名称

//    BOOL result = [UIImagePNGRepresentation() writeToFile: filePath    atomically:YES]; //保存成功会返回YES

NSLog(@"paths:%@    %@",paths,filePath);

[UIImagePNGRepresentation(image)writeToFile:filePathatomically:YES];

}

}

});

}failure:^(NSError*error) {

NSLog(@"加载启动页广告失败");

}];

14.常量

常量是容易重复被使用和无需通过查找和代替就能快速修改值。常量应该使用static来声明而不是使用#define,除非显式地使用宏。

应该:

[cpp] view plaincopy

1 static NSString * const RWTAboutViewControllerCompanyName = @"RayWenderlich.com";

2 static CGFloat const RWTImageThumbnailHeight = 50.0;

不应该:

[cpp] view plaincopy

1 #define CompanyName @"RayWenderlich.com"

2 #define thumbnailHeight 2

15.UITextField 限制输入数字

限制只能输入特定的字符

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

NSCharacterSet *cs;

cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];

NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串

BOOL canChange = [string isEqualToString:filtered];

return canChange;

}

上面那个NUMBERS是一个宏,可以在文件顶部定义:

#define NUMBERS @”0123456789\\n” (这个代表可以输入数字和换行,请注意这个\\n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件,因为你自己限制不让输入\\n,好惨,我在项目中才发现的。)

所以,如果你要限制输入英文和数字的话,就可以把这个定义为:

#define kAlphaNum   @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。

当然,你还可以在以上方法return之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。

限制只能输入一定长度的字符

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

{  //string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反

if ([string isEqualToString:@"\\n"])  //按会车可以改变

{

return YES;

}

NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容

if (self.myTextField == textField)  //判断是否时我们想要限定的那个输入框

{

if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警告

textField.text = [toBeString substringToIndex:20];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];

[alert show];

return NO;

}

}

return YES;

}

16.当用masonry做约束 需要获取约束之后的具体的frame值你需要这么做。

调用layoutIfNeeded。

期待接下来的更新吧 小伙子。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容