iOS 零碎知識點彙總

記錄一下零碎的知識點,不定時更新

算是 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; // 居中,還可以居於左右


// 內容偏移,對應X軸,Y軸,Z軸

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];





UIScrollView

// 不顯示左右滾動條
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];













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章