1.修改UILable的文本行間距
NSMutableAttributedString *attributedString =
[[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];
//調(diào)整行間距
[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 應(yīng)用直接退出
- (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];
// 設(shè)置屬性修改字體顏色 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];
}
// 設(shè)置選中和未選中字體顏色
[[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.設(shè)置狀態(tài)欄背景為任意的顏色
- (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選中狀態(tài)的顏色
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
12.關(guān)于右劃返回上一級
自定義leftBarButtonItem后無法啟用系統(tǒng)自帶的右劃返回可以再設(shè)置以下代碼
self.navigationController.interactivePopGestureRecognizer.delegate = self;
13.去掉導(dǎo)航欄下邊的黑線
[self.navigationController.navigationBar setShadowImage:[UIImage new]];//用于去除導(dǎo)航欄的底線黍析,也就是周圍的邊線
14.去掉UITableView的section的粘性帅刀,使其不會懸停框都。
//有時候使用UITableView所實現(xiàn)的列表,會使用到section,但是又不希望它粘在最頂上而是跟隨滾動而消失或者出現(xiàn)
- (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的調(diào)試方法 ?http://blog.csdn.net/yst19910702/article/details/51576562
16.一段代碼執(zhí)行的時間
#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