iOS - 项目中经常用到的小方法大全(持续更新)

//pop到底个之前的第x页面  
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];  
  
//跳转到该应用的系统设置  
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];  
if([[UIApplication sharedApplication] canOpenURL:url]) {  
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];  
    [[UIApplication sharedApplication] openURL:url];  
}  
  
//设置父视图透明度而不影响子视图  
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];  
  
//改变textfield占位字颜色  
[textField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];  
  
//修改textField字间距  
NSDictionary *attrsDictionary = @{NSFontAttributeName: textField.font,  
                                  NSKernAttributeName:[NSNumber numberWithFloat:10.0f]};  
textField.attributedText=[[NSAttributedString alloc]initWithString:textField.text  
                                                        attributes:attrsDictionary];  
//textview光标在第一行  
self.automaticallyAdjustsScrollViewInsets = NO;  
  
//给view某个角设置圆角  
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_confirmButton.bounds  
                                               byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)  
                                                     cornerRadii:CGSizeMake(5,5)];  
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];  
maskLayer.frame = _confirmButton.bounds;  
maskLayer.path = maskPath.CGPath;  
_confirmButton.layer.mask = maskLayer;  
   
//限制中英文字符串长度  
- (int)convertToInt:(NSString *)strtemp {  
    int strlength = 0;  
    charchar *p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];  
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {  
        if (*p) {  
            p++;  
            strlength++;  
        } else {  
            p++;  
        }  
    }  
    return (strlength+1)/2;  
}  
  
//判断字符串是否为纯数字  
if (![self isPureInt:self.inputTextField.text]) {  
    WBShowToastMessage(@"请输入正确的QQ号");  
    return;  
}  
  
- (BOOL)isPureInt:(NSString*)string{  
    NSScanner *scan = [NSScanner scannerWithString:string];  
    int val;  
    return [scan scanInt:&val] && [scan isAtEnd];  
}  
  
// 密码不能包含中文或者中文符号  
for (int i = 0; i < _userPwdTextField.text.length; i++) {  
    NSRange range = NSMakeRange(i, 1);  
    NSString *subString = [_userPwdTextField.text substringWithRange:range];  
    const charchar *cString = [subString UTF8String];  
    if (strlen(cString) == 3) {  
        [self shake:_pwdBgImage];  
        _reminderLabel.text = @"密码不能包含中文或者中文符号";  
        _reminderLabel.textColor = ReminderColor; return;  
    }  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容