记录一下零碎的知识点,不定时更新
算是 Android 零碎知识点汇总 的兄弟篇吧
控件相关
通用
// 设置圆角
<span style="font-size:12px;">myLabel.layer.cornerRadius = 8; // 弧度
myLabel.layer.masksToBounds = YES;// 子视图如果超过了当前视图的区域,超出的部分就会被裁掉</span>
// 设置边框
<span style="font-size:12px;">myLabel.layer.borderWidth = 0.5f; // 边框粗细
myLabel.layer.borderColor = [UIColor grayColor].CGColor; // 边框颜色<strong>
</strong></span>
// 阴影
<span style="font-size:12px;">// drop shadow
[myLabel.layer setShadowColor:[UIColor blackColor].CGColor];
[myLabel.layer setShadowOpacity:0.8];
[myLabel.layer setShadowRadius:3.0];
[myLabel.layer setShadowOffset:CGSizeMake(2.0, 2.0)];</span>
UILabel
// 设置文本删除线
myLabel.attributedText = [[NSAttributedString alloc] initWithString:@"测试数据" attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
// 显示HTML字符串
NSString *htmlString = @"html字符串'"
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
myLabel.attributedText = attrStr;
myLabel.textAlignment = NSTextAlignmentCenter; // 居中,还可以居于左右
myLabel.layer.sublayerTransform = CATransform3DMakeTranslation(5.0f, 0.0f, 0.0f);
// 自适应内容行数
myLabel.numberOfLines = 0;
[myLabel sizeToFit];
UITextField
// 设置 placeholder 颜色
tfPhone.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"text" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];
UIImageView
// 图片拉伸,使用 stretchableImageWithLeftCapWidth:AAA topCapHeight:BBB
// 注意拉伸的位置为 左起AAA+1 位置1像素,以及上起BBB+1的位置1像素,其它位置像素不变
imgBg.image = [[UIImage imageNamed:@"loading.jpg"] stretchableImageWithLeftCapWidth:1 topCapHeight:1];
// 不显示左右滚动条
scrollView.showsHorizontalScrollIndicator = NO;
// 不显示上下滚动条
scrollView.showsVerticalScrollIndicator = NO;
// 按页滑动
scrollView.pagingEnabled = YES;
UITableView
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 隐藏分割线
// 滚动到顶部
// 滑动到顶部
[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[tableView setContentOffset:CGPointZero animated:NO];
// 获取所点目录对应的indexPath值
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
// 让table滚动到对应的indexPath位置
[tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
// 隐藏多余的分割线
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[myTableView setTableFooterView:view];
// 这样更简单
[self.tableView setTableFooterView:[UIView new]];
// 获取指定项cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UICollectionView
// 获取指定项
UICollectionViewCell *cell = [collectionViewClass cellForItemAtIndexPath:indexPath];
布局视图相关
一、navigationController
// 在 AppDelegate.m 文件中修改,达到全局改变效果
// 导航栏背景色
[[UINavigationBar appearance] setBarTintColor:MAIN_COLOR];
// 不启用半透明效果
[[UINavigationBar appearance] setTranslucent:NO];
// 导航按钮为白色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// 导航文本白色
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
// 修改状态栏颜色
// Info.plist 文件中 View controller-based status bar appearance 设置为NO
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// 导航 back按钮不显示文本
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
// 修改导航栏文本颜色,在UIViewController中修改
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
// 设置导航栏透明
- (void)viewWillAppear:(BOOL)animated { // 修改为透明
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
self.navigationController.navigationBar.translucent = YES;
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated { // 恢复默认主题
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
[self.navigationController.navigationBar setBarTintColor:MAIN_COLOR];
[super viewWillDisappear:animated];
}
二、屏幕宽高
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
三、导航栏、状态栏高度
// 状态栏高度
[[UIApplication sharedApplication] statusBarFrame].size.height
// 导航栏高度
self.navigationController.navigationBar.frame.size.height;
其它相关
一、获取版本号
// 获取版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
二、获取全部系统字体
NSArray *familyNames = [UIFont familyNames];
for( NSString *familyName in familyNames ){
printf( "Family: %s \n", [familyName UTF8String] );
NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
for( NSString *fontName in fontNames ){
printf( "\tFont: %s \n", [fontName UTF8String] );
}
}
三、判断代理有木有实现某方法
if (self.delegate || [self.delegate respondsToSelector:@selector(test:string)]) {
// YES;
}
SEL sel = @selector(rightButtonClick);
if ([_arrayTabView[index] respondsToSelector:sel]) {
[_arrayTabView[index] performSelector:sel withObject:self]; //调用选择器方法
}
四、常用宏
// 宏 方法
// 屏幕的判定
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
// 屏幕宽高
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
// 状态栏+导航栏高度
#define STATUS_NAV_HEIGHT [[UIApplication sharedApplication]statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height
// 手机型号
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH >= 736.0)
//#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
// IOS版本
#define IS_IOS7 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]==7)
#define IS_IOS8 ([[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8)
真机或模拟器
//#if TARGET_IPHONE_SIMULATOR//模拟器
//
//#elif TARGET_OS_IPHONE//真机
//
//#endif
// 颜色取值
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
// 自定义Log
#ifdef DEBUG //调试阶段
#define DEBUGLog(...) NSLog(__VA_ARGS__)
#else //发布阶段
#define DEBUGLog(...)
#endif
五、打印特殊值
NSLog(@"rect %@", NSStringFromCGRect(self.view.frame));
NSLog(@"rect %@", NSStringFromCGRect(self.view.bounds));
NSLog(@"size %@", NSStringFromCGSize(self.view.frame.size));
NSLog(@"point %@", NSStringFromCGPoint(self.view.frame.origin));
// 其它类似
NSArray
// 让数组中的每个对象执行指定方法
<pre name="code" class="objc">// 无参数
[mainView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 带个参数
[tabController.tabBar.items makeObjectsPerformSelector:@selector(setEnabled:) withObject:false];