ios开发:小技巧

0、键盘顶端颜色,文字的修改

textField2.attributedPlaceholder=[[NSAttributedString alloc] initWithString:[[TitleUtility sharedInstance] getTitleWithKey:@"Password"] attributes:@{NSForegroundColorAttributeName: color}];

1.获取view的名字

[NSString stringWithUTF8String:object_getClassName(smallView)

2.删除navgationBar最下面的黑色分割线

(UIImageView)
    UIImageView *imgV = [self.navigationController.navigationBar.subviews[0] subviews][0];
    imgV.hidden = YES;

3.cell右边标志符的颜色改变

self.accessoryType = UITableViewCellAccessoryCheckmark;
self.tintColor = [UIColor colorWithRed:140.0/255.0 green:197.0/255.0  blue:66.0/255.0  alpha:1];

4.slider滑块固定大小分段滑动

/************分段滑动的方法*****************/
        // These number values represent each slider position
        numbers = @[@(500), @(1000), @(1500), @(2000), @(2600)];
        // slider values go from 0 to the number of values in your numbers array
        NSInteger numberOfSteps = ((float)[numbers count] - 1);
        slider.maximumValue = numberOfSteps;
        slider.minimumValue = 0;
    
        // As the slider moves it will continously call the -valueChanged:
        slider.continuous = NO; // NO makes it call only once you let go
        [slider addTarget:self
                   action:@selector(valueChanged:)
         forControlEvents:UIControlEventValueChanged];
    }
    - (void)valueChanged:(UISlider *)sender {
        // round the slider position to the nearest index of the numbers array
        NSUInteger index = (NSUInteger)(slider.value + 0.5);
        [slider setValue:index animated:YES];
        NSNumber *number = numbers[index]; // <-- This numeric value you want
        NSLog(@"sliderIndex: %i", (int)index);
        NSLog(@"number: %@", number);
    }
/************分段滑动的方法*****************/

5.设置启动页的时间

[NSThread sleepForTimeInterval:3.0];//设置启动页面时间

6,程序自动关闭

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [theView removeFromSuperview];
        exit(0);
      });

7.数字用逗号隔开每3位

+(NSString *)countNumAndChangeformat:(NSString *)num
{
    int count = 0;
    long long int a = num.longLongValue;
    while (a != 0)
    {
        count++;
        a /= 10;
    }
    NSMutableString *string = [NSMutableString stringWithString:num];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(string.length - 3, 3);
        NSString *str = [string substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [string deleteCharactersInRange:rang];
    }
    [newstring insertString:string atIndex:0];
    return newstring;
}

8.修改searchBar取消按钮的文字和颜色

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    NSLog(@"%@",[searchBar subviews]);
    [(UIButton *)searchBar.subviews[0] setTintColor:[UIColor whiteColor]];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            NSLog(@"%@",view.subviews);
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:[[TitleUtility sharedInstance] getTitleWithKey:@"alertCancel"] forState:UIControlStateNormal];
        }
    }
}

9.数组元素按字母排序

_enStateArr = [_stateDic allKeys];
    _enStateArr = [_enStateArr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSComparisonResult result = [obj1 compare:obj2];
        return result==NSOrderedDescending;
    }];

10.控件的晃动

#import 

 -(void)loadShakeAnimationForView:(UIView*)view  
 {  
    CALayer *lbl = [view layer];  
    CGPoint posLbl = [lbl position];  
    CGPoint y = CGPointMake(posLbl.x-10, posLbl.y);  
    CGPoint x = CGPointMake(posLbl.x+10, posLbl.y);  
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];  
    [animation setTimingFunction:[CAMediaTimingFunction  
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];  
    [animation setFromValue:[NSValue valueWithCGPoint:x]];  
    [animation setToValue:[NSValue valueWithCGPoint:y]];  
    [animation setAutoreverses:YES];  
    [animation setDuration:0.08];  
    [animation setRepeatCount:3];  
    [lbl addAnimation:animation forKey:nil];  
    } 

11.判断字符串中是否包含某个字符

//判断字符是否包含某字符串;
    NSString *string = @"hello,shenzhen,martin";
    
    //字条串是否包含有某字符串
    if ([string rangeOfString:@"martin"].location == NSNotFound) {
        NSLog(@"string 不存在 martin");
    } else {
        NSLog(@"string 包含 martin");
    }

    //字条串开始包含有某字符串
    if ([string hasPrefix:@"hello"]) {
        NSLog(@"string 包含 hello");
    } else {
        NSLog(@"string 不存在 hello");
    }
    
    //字符串末尾有某字符串;
    if ([string hasSuffix:@"martin"]) {
        NSLog(@"string 包含 martin");
    } else {
        NSLog(@"string 不存在 martin");
    }
在iOS8以后,还可以用下面的方法来判断是否包含�某字符串:
//在iOS8中你可以这样判断
    NSString *str = @"hello world";
    if ([str containsString:@"world"]) {
        NSLog(@"str 包含 world");
    } else {
        NSLog(@"str 不存在 world");

    }

12.去掉返回手势

// 去掉返回按钮
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:(UIBarButtonItemStyleDone) target:self action:nil];
// 去掉返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

13.字典转换成字符串

- (NSString *)parseParams:(NSDictionary *)params{
    NSString *keyValueFormat;
    NSMutableString *result = [NSMutableString new];
    NSMutableArray *array = [NSMutableArray new];
    //实例化一个key枚举器用来存放dictionary的key
    NSEnumerator *keyEnum = [params keyEnumerator];
    id key;
    int i=0;
    while (key = [keyEnum nextObject]) {
        if (i==0) {
            keyValueFormat = [NSString stringWithFormat:@"%@=%@",key,[params valueForKey:key]];
        }else
        {
            keyValueFormat = [NSString stringWithFormat:@"&%@=%@",key,[params valueForKey:key]];
        }
        
        //        NSLog(@"%@",keyValueFormat);
        [result appendString:keyValueFormat];
        [array addObject:keyValueFormat];
        i++;
    }
    
    return result;
}

14.按钮文字居左

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 这行代码,把按钮的内容(控件)
的对齐方式修改为水平左对齐,但是这们会紧紧靠着左边,不好看,所以我们还可以修改属性:
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了

15、四舍五入

-(float)roundFloat:(float)price{
    return (floorf(price*100 + 0.5))/100;
}

16.两个时间比较大小

/************
 日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];
 ***********/
+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dt1 = [[NSDate alloc]init];
    NSDate *dt2 = [[NSDate alloc]init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1;break;
            //date02比date01小
        case NSOrderedDescending: ci=-1;break;
            //date02=date01
        case NSOrderedSame: ci=0;break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1);break;
    }
    return ci;
}

17.修改输入框提示文字的颜色

UIColor *color = [UIColor whiteColor];
  userNameTextField.attributedPlaceholder = [[NSAttributedString alloc]
      initWithString:@"请输入手机号"
          attributes:@{NSForegroundColorAttributeName : color}];

你可能感兴趣的:(ios开发:小技巧)