iOS开发比较有用的代码段

限制字母、数字、符号等其他非中文字符的长度大家应该都知道咋整,但如果是中文输入法,这就比较蛋疼了。举个例子,限制长度为5,输入“我爱写代码”,当已经输入“我爱写”两字,然后输入“代码”时就会出现输入后面几位汉字被字母占用位置而输入不进去。解决办法:

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *toBeString = textView.text;
    // 获取输入法
    NSString *lang = textView.textInputMode.primaryLanguage;
    // 如果输入法为中文
    if ([lang isEqualToString:@"zh-Hans"]) {
        // 这个range就是指输入的拼音还没有转化成中文时的range
        // 如果没有,就表示已经转成中文了
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange && toBeString.length > 5) {
            textView.text = [toBeString substringToIndex:5];
        }
    } else if (toBeString.length > 5) {
        textView.text = [toBeString substringToIndex:5];
    }
}
复制代码

#NSDate转NSString 日期转成字符串。

+(NSString *)stringFromDate:(NSDate *)date
{
//    //获取系统当前时间
//    NSDate *currentDate = [NSDate date];
    //用于格式化NSDate对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式:zzz表示时区
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:date];
    //输出currentDateString
    NSLog(@"%@",currentDateString);
}
复制代码

NSDate对象包含两个部分,日期(Date)和时间(Time)。格式化的时间字符串主要也是针对日期和时间。NSDateFormatter是一个很常用的类,用于格式化NSDate对象,支持本地化的信息。 NSDateFormatter常用格式有:

yyyy-MM-dd HH:mm:ss.sss
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyy
复制代码

NSDateFormatter格式化参数如下:

G:公元时代,例如AD公元
yy:年的后2位
yyyy:完整年
MM:月,显示为1-12
MMM:月,显示为英文月份简写,如 Jan
MMMM:月,显示为英文月份全称,如 Janualy
dd:日,2位数表示,如02
d:日,1-2位显示,如 2、11
EEE:简写星期几,如Sun
EEEE:全蝎星期几,如Sunday
aa:上下午,AM/PM
H:时,24小时制,0-23
K:时,12小时制,0-11
m:分,1-2位
mm:分,2位
s:秒,1-2位
ss:秒,2位
S:毫秒
复制代码

#NSString转NSDate 字符串转日期

+ (NSDate *)dateFromString:(NSString *)string
{
    //设置转换格式  
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //NSString转NSDate
    NSDate *date = [formatter dateFromString:dateString];
    return date;
}
复制代码

参考网站:http://www.superqq.com/blog/2015/06/26/nsdatehe-nsstringxiang-hu-zhuan-huan/ 日历和时间的计算可参考:http://www.jianshu.com/p/692bb0a7269c

#iOS 监听键盘的显示与隐藏,并实现输入框紧紧跟随在键盘的上方

// 在-viewDidLoad方法中注册通知,监听键盘的弹出和收回
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification 
                                           object:nil];
复制代码

###pragma mark 键盘显示的监听方法

-(void) keyboardWillShow:(NSNotification *) note
{
    // 获取键盘的位置和大小
CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    // 获取输入框的位置和大小
CGRect containerFrame = _inputView.frame;
    // 计算出输入框的y坐标
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    
    // 动画改变位置
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        // 更改输入框的位置
        _inputView.frame = containerFrame;
    }];
}
复制代码

###pragma mark 键盘隐藏的监听方法

-(void) keyboardWillHide:(NSNotification *) note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
 
    // 获取输入框的位置和大小
CGRect containerFrame = _inputView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
    
    // 动画改变位置
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        // 更改输入框的位置
        _inputView.frame = containerFrame;
    }];
}
复制代码

_inputView便是存放输入框和View,就是跟随键盘上下移动的UIView ///

转载于:https://juejin.im/post/5a3125f5f265da430d580373

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值