iOS 开发常用小技巧

1.修改UILable的文本行间距

NSMutableAttributedString *attributedString =

[[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];

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

[paragraphStyle setLineSpacing:3];

//调整行间距

[attributedString addAttribute:NSParagraphStyleAttributeName

value:paragraphStyle

range:NSMakeRange(0, [self.contentLabel.text length])];

self.contentLabel.attributedText = attributedString;


2.NSString 过滤特殊字符

// 定义一个特殊字符的集合

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:

@"@/:;()¥「」"、[]{}#%-*+=_\|~<>$€^•'@#$%^&*()_+'""];

// 过滤字符串的特殊字符

NSString *newString = [trimString stringByTrimmingCharactersInSet:set];


3.让 iOS 应用直接退出

- (void)exitApplication {

AppDelegate *app = [UIApplication sharedApplication].delegate;

UIWindow *window = app.window;

[UIView animateWithDuration:1.0f animations:^{

window.alpha = 0;

} completion:^(BOOL finished) {

exit(0);

}];

}


4.NSArray 快速求总和 最大值 最小值 和 平均值

NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];

CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];

CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];

CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];

CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];

NSLog(@"%fn%fn%fn%f",sum,avg,max,min);


5.修改 Label 中不同文字颜色

[self editStringColor:self.label.text editStr:@"好" color:[UIColor blueColor]];


- (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {

// string 为整体字符串, editStr 为需要修改的字符串

NSRange range = [string rangeOfString:editStr];

NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];

// 设置属性修改字体颜色 UIColor 与大小 UIFont

[attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];

self.label.attributedText = attribute;

}


6.修改 Tabbar Item 的属性

// 修改标题位置

self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);

// 修改图片位置

self.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);

// 批量修改属性

for (UIBarItem *item in self.tabBarController.tabBar.items) {

[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:

[UIFont fontWithName:@"Helvetica" size:19.0], NSFontAttributeName, nil]

forState:UIControlStateNormal];

}

// 设置选中和未选中字体颜色

[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

// 未选中字体颜色

[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal];

// 选中字体颜色

[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cyanColor]} forState:UIControlStateSelected];


7.修改 UITextField 中 Placeholder 的文字颜色和大小

[text setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

[text setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];


8.判断 view 是不是指定视图的子视图

BOOL isView =  [textView isDescendantOfView:self.view];


9.设置状态栏背景为任意的颜色


- (void)setStatusColor

{

UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];

statusBarView.backgroundColor = [UIColor orangeColor];

[self.view addSubview:statusBarView];

}


10.去除字符串中所有的空格

[str stringByReplacingOccurrencesOfString:@" " withString:@""]



11.修改tableViewCell选中状态的颜色

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];

cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];


12.关于右划返回上一级

自定义leftBarButtonItem后无法启用系统自带的右划返回可以再设置以下代码

self.navigationController.interactivePopGestureRecognizer.delegate = self;


13.去掉导航栏下边的黑线

[self.navigationController.navigationBar setShadowImage:[UIImage new]];//用于去除导航栏的底线,也就是周围的边线

14.去掉UITableView的section的粘性,使其不会悬停。

//有时候使用UITableView所实现的列表,会使用到section,但是又不希望它粘在最顶上而是跟随滚动而消失或者出现

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if (scrollView == _tableView) {

CGFloat sectionHeaderHeight = 36;

if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >=

0) {

scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

} else if (scrollView.contentOffset.y >= sectionHeaderHeight) {

scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);

}

}

}


15.常见bug的调试方法  http://blog.csdn.net/yst19910702/article/details/51576562


16.一段代码执行的时间

#define TICK NSDate *startTime = [NSDate date]

#define TOCK NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])


17.NSLog的处理方式


// 保证 #ifdef 中的宏定义只会在 OC 的代码中被引用

// 否则,一旦引入 C/C++ 的代码或者框架,就会出错!

#ifdef __OBJC__

#ifdef DEBUG

#define NSLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

#else

#define NSLog(...)

#endif

#endif

你可能感兴趣的:(iOS 开发常用小技巧)