zoukankan      html  css  js  c++  java
  • iOS开发系列之四

    // 初始化输入框并设置位置和大小
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 300, 180)];
    // 设置预设文本
    textView.text = @"";
    // 设置文本字体
    textView.font = [UIFont fontWithName:@"Arial" size:16.5f];
    // 设置文本颜色
    textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];
    // 设置文本框背景颜色
    textView.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];
    // 设置文本对齐方式
    textView.textAlignment = NSTextAlignmentLeft;
    
    // iOS7中文本对齐方式有下面几种:
    //    enum {
    //        NSTextAlignmentLeft      = 0,  左对齐,默认
    //        NSTextAlignmentCenter    = 1,  居中对齐
    //        NSTextAlignmentRight     = 2,  右对齐
    //        NSTextAlignmentJustified = 3,  在一个段落的最后一行自然对齐
    //        NSTextAlignmentNatural   = 4,  默认对齐方式
    //    } NSTextAlignment;
    
    // 设置自己主动纠错方式
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    
    // 自己主动纠错方式有下面几种:
    //    enum {
    //        UITextAutocorrectionTypeDefault,  默认
    //        UITextAutocorrectionTypeNo,       不自己主动纠错
    //        UITextAutocorrectionTypeYes,      自己主动纠错
    //    } UITextAutocorrectionType;
    
    // 设置自己主动大写方式
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    // 自己主动大写方式有下面几种:
    //    enum {
    //        UITextAutocapitalizationTypeNone,           不自己主动大写
    //        UITextAutocapitalizationTypeWords,          单词首字母大写
    //        UITextAutocapitalizationTypeSentences,      句子的首字母大写
    //        UITextAutocapitalizationTypeAllCharacters,  全部字母都大写
    //    } UITextAutocapitalizationType;
    
    // 设置键盘的样式
    textView.keyboardType = UIKeyboardTypeDefault;
    
    // 键盘样式有下面几种:
    //    enum {
    //        UIKeyboardTypeDefault,                默认键盘,支持全部字符
    //        UIKeyboardTypeASCIICapable,           支持ASCII的默认键盘
    //        UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
    //        UIKeyboardTypeURL,                    仅仅支持URL字符的URL键盘,支持.combutton
    //        UIKeyboardTypeNumberPad,              数字键盘
    //        UIKeyboardTypePhonePad,               电话键盘
    //        UIKeyboardTypeNamePhonePad,           支持输入人名的电话键盘
    //        UIKeyboardTypeEmailAddress,           电子邮件键盘
    //        UIKeyboardTypeDecimalPad,             有数字和小数点的数字键盘
    //        UIKeyboardTypeTwitter,                优化的键盘,方便输入@、#字符
    //        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
    //    } UIKeyboardType;
    
    // 设置return键样式
    textView.returnKeyType = UIReturnKeyDefault;
    
    // return键有下面几种样式:
    //    enum {
    //        UIReturnKeyDefault,        默认,灰色button。标有Return
    //        UIReturnKeyGo,             标有Go的蓝色button
    //        UIReturnKeyGoogle,         标有Google的蓝色button,用于搜索
    //        UIReturnKeyJoin,           标有Join的蓝色button
    //        UIReturnKeyNext,           标有Next的蓝色button
    //        UIReturnKeyRoute,          标有Route的蓝色button
    //        UIReturnKeySearch,         标有Search的蓝色button
    //        UIReturnKeySend,           标有Send的蓝色button
    //        UIReturnKeyYahoo,          标有Yahoo的蓝色button
    //        UIReturnKeyYahoo,          标有Yahoo的蓝色button
    //        UIReturnKeyEmergencyCall,  紧急呼叫button
    //    } UIReturnKeyType;
    
    // 设置能否够拖动
    textView.scrollEnabled = YES;
    // 设置代理
    textView.delegate = self;
    
    // 自己定义文本框placeholder
    tip = [[UILabel alloc] initWithFrame:CGRectMake(16, 14, 320, 25)];
    tip.text = @"您的意见是我们前进的最大动力,谢谢!";
    tip.font = [UIFont fontWithName:@"Arial" size:16.5f];
    tip.backgroundColor = [UIColor clearColor];
    tip.enabled = NO;
    
    // 自己定义文本框字数统计
    count = [[UILabel alloc] initWithFrame:CGRectMake(270, 170, 35, 20)];
    count.text = @"240";
    count.textAlignment = NSTextAlignmentRight;
    count.font = [UIFont fontWithName:@"Arial" size:15.0f];
    count.backgroundColor = [UIColor clearColor];
    count.enabled = NO;
    
    // 显示文本框及相关控件
    [self.view addSubview:feedback];
    [self.view addSubview:tip];
    [self.view addSubview:count];
    
    // 限制输入文本长度
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
        if (range.location < 240)
        {
            return  YES;
        } else {
            return NO;
        }
    }
    
    // 自己定义文本框placeholder
    - (void)textViewDidChange:(UITextView *)textView
    {
        count.text = [NSString stringWithFormat:@"%d", 240 - feedback.text.length];
        if (textView.text.length == 0)
        {
            tip.text = @"您的意见是我们前进的最大动力,谢谢!";
        } else {
            tip.text = @"";
        }
    }

    本文固定链接:http://www.itechzero.com/ios-development-series-four-uitextview-usage-summary.html,转载请注明出处。

  • 相关阅读:
    试题 基础练习 Sine之舞
    试题 基础练习 Huffuman树
    试题 基础练习 完美的代价
    支付宝支付功能, 创建订单并生成支付链接的接口, 后端支付宝异步回调接口
    GenericAPIView, drf内置的基础分页器和偏移分页器的使用, 过滤器, django-filter插件实现区间分类
    celery, 数据库分表
    redis
    注册, 校验手机号接口, 手机号注册接口, 全局配置drf-jwt身份认证, 局部配置SendSmsAPIView类的频率认证, Vue操作Cookie
    登录, 发送验证码接口, 对官方提供的短信SDK进行二次封装
    pycharm全局搜索快捷键: ctrl + n, Xadmin的使用, 前端Banner小组件, 后端控制轮播图展示的数量, git
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7121269.html
Copyright © 2011-2022 走看看