iOS使用小技巧整理

简介

日常开发过程中积累的小经验,分享给大家!

1. 四舍五入用法

float numToRound;

int result;

numToRound = 5.61;

result = (int)roundf(numToRound);//roundf函数

NSLog(@"roundf(%.2f) = %d",numToRound,result);

2. self.view.widow

//判断View是否在当前的window上显示,可以提升性能(看斯坦福那个老头视频里学到的)

if (self.view.widow)

3. 屏幕大小

[[UIScreen mainScreen] bounds];//包括状态栏

[[UIScreen mainScreen] applicationFrame];//不包括状态栏

4. 画填充圆

CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor whiteColor] set];

CGContextFillRect(context, rect);

CGContextAddEllipseInRect(context, frame);

[[UIColor colorWithRed:214/255.0 green:19/255.0 blue:20/255.0 alpha:1] set];

CGContextFillPath(context);

5. 方法延迟执行

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{

[activity stopAnimating];

self.netOffImage.alpha = 1;

self.netOffBtn.alpha = 1;

self.netOffLable.alpha = 1;

});

6. 手动退出程序( exit(0) )

- (void)exitApplication {

AppDelegate *app = [UIApplication sharedApplication].delegate;

UIWindow *window = app.window;

[UIView animateWithDuration:1.0f animations:^{

window.alpha =0;

window.frame = CGRectMake(0, window.bounds.size.width,0,0);

} completion:^(BOOL finished) {

exit(0);

}];

}

7. lable加中划线

//1.调用super的方法就可以拿到值

[super drawRect:rect];

//2.获取到lable字体的尺寸

CGSize size = [self.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font,NSFontAttributeName,nil]];

注:- (CGSize)sizeWithAttributes:(NSDictionary*)attrs;这个方法是iOS7之后的方法代替了iOS6中过时了的- (CGSize)sizeWithFont:(UIFont*)font

3.画线

UIRectFill(CGRectMake(0, size.height*0.5, size.width,1));

如果你想设置线的颜色可以[[UIColor redColor] set];不过需要注意的是要在画线之前设置。

8. plist文件里的"Bundle versions string, short"跟"Bundle version"的区别及作用

1,Version是显示对外的版本号,(itunesconect和Appstore用户可以看到),对应O-C中获取version的值:

[[[NSBundlemainBundle]infoDictionary]valueForKey:@"CFBundleShortVersionString"];

该版本的版本号是三个分隔的整数组成的字符串。第一个整数代表重大修改的版本,如实现新的功能或重大变化的修订。第二个整数表示的修订,实现较突出的特点。第三个整数代表维护版本

例如:1.0.12或者1.2.3等等

2,build别人看不到,只有开发者自己才能看到,相当于内部版本号。【更新版本的时候,也要高于之前的build号】对应获取方式:

[[[NSBundlemainBundle]infoDictionary]valueForKey:@"CFBundleVersion"];

标示(发布或者未发布)的内部版本号。这是一个单调增加的字符串,包括一个或者多个分割的整数。

9. button 的内容位置

在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets

@property(nonatomic)          UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero

@property(nonatomic)          UIEdgeInsets titleEdgeInsets;                // default is UIEdgeInsetsZero

@property(nonatomic)          BOOL         reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance

@property(nonatomic)          UIEdgeInsets imageEdgeInsets;                // default is UIEdgeInsetsZero

UIEdgeInsetsMake

里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在button的正中央

[leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)]; //右移4

10. 设置状态栏

[[UIApplication sharedApplication] setStatusBarHidden:NO];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

11. UINavigationController push不平滑的解决方法

- (void)loadView

{

[super loadView];

[self.navigationController setNavigationBarHidden:NO animated:YES];

//self.navigationController.navigationBar.hidden = NO;

}

12. 隐藏导航栏底部的黑线

NSArray *arry = [self.navigationController.navigationBar subviews];

for (UIView *view in arry) {

NSString *viewName = NSStringFromClass([view class]);

if ([viewName isEqualToString:@"_UINavigationBarBackground"]) {

UIImageView *line = [view valueForKey:@"_shadowView"];

line.hidden = isHidden;

}}

13. iOS转义符

\r\n

14. 使用NSAttributedString显示HTML

在app中使用Webviews有时会让人非常沮丧,即使只是显示少量的HTMLneirong ,Webviews也会消耗大量的内容。现在iOS7让这些变得简单了,你可以从用少量代码在HTML文件中创建一个NSAttributedString,比如:

NSString *html = @"Wow! Now iOS can create 

NSAttributedString

 from HTMLs!";

NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]

options:options documentAttributes:nil error:nil];

15. 设置tableView Cell之间的间隙

- (void)setFrame:(CGRect)frame

{

if (frame.size.height < 70) {

//frame.origin.x += 10;

[super setFrame:frame];

return;

}

frame.size.height -= 10;

//frame.origin.x += 10;

frame.origin.y += 10;

//frame.size.width -= 20;

[super setFrame:frame];

}

你可能感兴趣的:(iOS使用小技巧整理)