小知識整理

工作中遇到的一些小知識,不定時更新,以免忘記,方便以後要用到時查找

1.獲取webView中的scrollView

    UIScrollView *tempView = (UIScrollView *)[_WebView.subviewsobjectAtIndex:0];

2.iOS9訪問http協議的URL需要修改

在Info.plist 配置中添加NSAppTransportSecurity—>NSAllowsArbitraryLoads—>YES
在 plist 文件裏顯示如下:
這裏寫圖片描述

3.設置導航條、狀態欄屬性(全局)

// 設置狀態欄字體顏色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// 設置導航欄的主題背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];
// 設置導航欄item顏色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// 設置導航欄標題顏色及字體
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:
[UIFont boldSystemFontOfSize:18]}];

4.Label文字自適應

NSString *text = @"your text";
CGSize textSize = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,100, textSize.width, textSize.height)];
textLabel.numberOfLines = 0;
CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];

5.頁面跳轉時隱藏顯示底部tabbar

self.hidesBottomBarWhenPushed = YES;   // 設置push下一頁面tabbar隱藏
[self.navigationController pushViewController:viewController animated:YES];
self.hidesBottomBarWhenPushed = NO;    // 設置pop回這一頁面tabbar不隱藏

6.判斷是否是郵箱號和手機號

+ (BOOL)validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailPred evaluateWithObject:email];
}

+ (BOOL)validateTelephone:(NSString *)phone
{
    NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9]))\\d{8}$";
    NSPredicate *phonePred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phonePred evaluateWithObject:phone];
}

7.設置Label文本前後不同屬性(字體大小、顏色等等)富文本

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20,100,300,100)];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:17];
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"字符串字體顏色和大小等等"];
    // 修改指定範圍字體單個屬性
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range: NSMakeRange(0, 3)];
   // 修改指定範圍字體多個屬性
    [attributedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(3, 5)];
    // 字體後添加自定義圖片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    attch.image = [UIImage imageNamed:@"imageName"];
    attch.bounds = CGRectMake(0, 0, 20, 20);
    NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attch];
    [attributedString appendAttributedString:str];
    label.attributedText = attributedString;
    [self.view addSubview:label];
    //    NSFontAttributeName
    //    字體
    //    NSParagraphStyleAttributeName
    //    段落格式
    //    NSForegroundColorAttributeName
    //    字體顏色
    //    NSBackgroundColorAttributeName
    //    背景顏色
    //    NSStrikethroughStyleAttributeName
    //    刪除線格式
    //    NSUnderlineStyleAttributeName
    //    下劃線格式
    //    NSStrokeColorAttributeName
    //    刪除線顏色
    //    NSStrokeWidthAttributeName
    //    刪除線寬度
    //    NSShadowAttributeName 
    //    陰影

8.URL地址需要編碼,當出現非ASCCI碼,需要對其他字符集做編碼處

 NSString * url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

9.nib和xib的區別

nib文件是xib文件編譯後的二進制文件

10.HTML/CSS/JavaScript網頁三劍客

 HTML:顯示,類似iOS中的UI部分
 CSS(層疊樣式表):定義樣式,類似iOS中的屬性值color,font
 JavaScript:腳本語言,定義交互,類似iOS中的action-target事件響應

11.生成指定範圍的隨機整數、隨機顏色

/*
 @brief 生成指定範圍的隨機整數
 @param min 下限(閉區間)
 @param max 上限(閉區間)
 @return 指定範圍的隨機數
 */
+ (int)getRandomIntBetweenMin:(int)min andMax:(int)max
{
    return arc4random() % (max - min + 1) + min;
}
/*
 @brief 生成隨機顏色
 @return UIColor對象的指針
 */
+ (UIColor *)getRandomColor
{
    double red = [self getRandomIntBetweenMin:0andMax:255] /255.0;
    double green = [self getRandomIntBetweenMin:0andMax:255] /255.0;
    double blue = [self getRandomIntBetweenMin:0andMax:255] /255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

12.改變圖片尺寸

/*
 @brief 改變圖片尺寸
  @param image 想要改變的圖片
  @param size 想要的圖片尺寸
 @return 改變尺寸後的圖片
 */
+ (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImage;
}

13.在ARC代碼中混編非ARC代碼

ios中如果arc和非arc文件混編,可以在build parses中展開Compile Sources,找到你你想要混編的文件名,如果是arc文件則雙擊添加-fobjc-arc,非arc文件則雙擊添加-fno-objc-arc

將整個項目改成非arc機制的方法:在build Settings中:把Objective-C Automatic Reference Counting設爲NO

發佈了44 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章