This article is mainly summarized in the development of the time, use some tips, I hope to help you, quickly complete some functions ~

1. Set navigationBar to be transparent instead of blurry.

[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

self.navigationBar.shadowImage = [UIImage new];

self.navigationBar.translucent = YES; 
Copy the code

2. Hide the navigation bar while scrolling

self.navigationController.hidesBarsOnSwipe = YES;
Copy the code

Dynamic hidden NavigationBar / / # # # 1. When we leave screen hand hide – (void) scrollViewWillEndDragging: (scrollView UIScrollView *) withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ if(velocity.y > 0) { [self.navigationController setNavigationBarHidden:YES animated:YES]; } else { [self.navigationController setNavigationBarHidden:NO animated:YES]; }} / / 2. In the process of sliding hide / / like a safari. (1) the self navigationController. HidesBarsOnSwipe = YES; (2)- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

CGFloat offsetY = scrollView.contentOffset.y + __tableView.contentInset.top; CGFloat panTranslationY = [scrollView.panGestureRecognizer translationInView:self.tableView].y; If (offsetY > 64) {if (panTranslationY > 0) {// Show [self navigationController setNavigationBarHidden: NO animated: YES]; } else {Copy the code

/ / sliding trend, hidden [self navigationController setNavigationBarHidden: YES animated: YES]; } } else { [self.navigationController setNavigationBarHidden:NO animated:YES]; }}

3. Calculation Method Time interval

#define TICK   CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define TOCK   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)
Copy the code

4. Mutual conversion of NSDate and NSString

-(NSString *)dateToString:(NSDate *)date {NSDateFormatter *matter = [[NSDateFormatter alloc] init]; // Set design format [matter setDateFormat:@" YYYY-MM-DD hh: MM :ss ZZZ "]; // Perform the conversionCopy the code

NSString *dateStr = [matter stringFromDate:date];        return dateStr;    }

-(NSDate *)stringToDate:(NSString *)dateStr {NSDateFormatter *matter = [[NSDateFormatter alloc] init]; // Set design format [matter setDateFormat:@” YYYY-MM-DD hh: MM :ss ZZZ “]; NSDate *date = [matter dateFromString:dateStr]; return date; }

5. Hide the thin line under the navigationBar

/ / but there are fine lines, which requires us to do further processing, remove the line, the following methods can be: the self. The navigationController. NavigationBar. ShadowImage = [UIImage new];Copy the code

6. Set navigationBar gradient transparency

//navigationBar is a composite view consisting of many controls, Then we can start from his internal [[self. NavigationController. NavigationBar subviews] objectAtIndex: 0]. Alpha = 0; Alpha can be set according to the offset of the scrollView to achieve gradient transparencyCopy the code

7. Check whether the NSString contains Chinese characters

 -(BOOL)isChinese:(NSString *)str{      
  NSString *match=@"(^[\u4e00-\u9fa5]+$)";     
Copy the code

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”SELF matches %@”, match];         return [predicate evaluateWithObject:str];    }

8. Determine whether the camera is available

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])        {
Copy the code

} else {UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@” Phone camera device is damaged “message:@”” delegate:nil CancelButtonTitle :@” confirm “otherButtonTitles:nil, nil]; [alertView show]; }

9. Limit the number of textFiled characters

##### add a notification to the controller [[NSNotificationCenter defaultCenter] addObserver:self Selector :@selector(textFieldEditChanged:) name:@”UITextFieldTextDidChangeNotification” object:nil];

##### implements the Method #pragma mark – Notification Method -(void)textFieldEditChanged:(NSNotification *)obj{UITextField *textField = (UITextField *)obj.object; NSInteger maxLength = MAX_STARWORDS_LENGTH; // Set the maximum input length NSString *toBeString = textField.text; if (toBeString.length > maxLength) { NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength]; if (rangeIndex.length == 1) { textField.text = [toBeString substringToIndex:maxLength]; } else { NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)]; textField.text = [toBeString substringWithRange:rangeRange]; }}}

There’s another way you could write it

UITextField *textField = (UITextField *)obj.object; NSString *toBeString = textField.text; // Get the highlighted partCopy the code

UITextRange *selectedRange = [textField markedTextRange]; UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0]; // If no selected word is highlighted, count and limit the entered word if (! position) { if (toBeString.length > MAX_STARWORDS_LENGTH) { NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH]; if (rangeIndex.length == 1) { textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH]; } else { NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)]; textField.text = [toBeString substringWithRange:rangeRange]; }}}

10. Encrypt a string using MD5

- (NSString *)md5:(NSString *)str{   
 const char *cStr = [str UTF8String];   
 unsigned char result[16];   
 CC_MD5(cStr, strlen(cStr), result);
 // This is the md5 call   
 return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",        result[0], result[1], result[2], result[3],result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ];
 }
Copy the code

11. Parent controls are translucent and child controls are opaque

When setting the background color of the parent control with colorWithAlphaComponent: method fuView. BackgroundColor = [[UIColor blackColor] colorWithAlphaComponent: 0.5];Copy the code

Add child controls directly to it

12. Calculate text height by text content /**

  • Computing text height
  • @ param text text
  • @param limitW Text width
  • @ param font font
  • @ param lineSpacing line height
  • @param lineHeightMultiple Line spacing
  • @param lineBreakMode Paragraph style
  • @return */
  • (CGSize)calculateSizeWithText:(NSString *)text limitWidth:(CGFloat)limitW font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = lineSpacing; paragraphStyle.lineHeightMultiple = lineHeightMultiple; paragraphStyle.lineBreakMode = lineBreakMode; NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle};

    CGSize size; size = [text boundingRectWithSize:CGSizeMake(limitW, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:cyZoneDocAttribute context:nil].size; return size; }

This is setting the rich text property of this UILabel /**

  • The rich text
  • @ param string of text
  • @ param font font
  • @ param lineSpacing line height
  • @param lineHeightMultiple Line spacing (1.5 times)
  • @param lineBreakMode Paragraph style
  • @return */
  • (NSMutableAttributedString *)getAttributedStringWithString:(NSString *)string font:(UIFont *)font lineSpacing:(CGFloat)lineSpacing lineHeightMultiple:(CGFloat)lineHeightMultiple lineBreakMode:(NSLineBreakMode )lineBreakMode{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.lineSpacing = lineSpacing; paragraphStyle.lineHeightMultiple = lineHeightMultiple; paragraphStyle.lineBreakMode = lineBreakMode; NSDictionary *cyZoneDocAttribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName: paragraphStyle}; [attributedString addAttributes:cyZoneDocAttribute range:NSMakeRange(0, [string length])]; return attributedString; }

13. Obtain unique identification of equipment

+ (NSString *)getDeviceId
{
    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@"com.xinghengedu"account:@"uuid"];
if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
{
    NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
    
    currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
    currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
    [SSKeychain setPassword: currentDeviceUUIDStr forService:@"com.xinghengedu"account:@"uuid"];
}
return currentDeviceUUIDStr;
}
Copy the code

14. Check that a string is all numbers

+ (BOOL)isPureInt:(NSString *)string{
NSScanner* scan = [NSScanner scannerWithString:string]; 
int val; 
return [scan scanInt:&val] && [scan isAtEnd];
}
Copy the code

When block is used, __weak __typeof(self)weakSelf = self; __strong __typeof(weakSelf)strongSelf = weakSelf;