iOS开发系列之四 - UITextView 用法小结

来源:互联网 发布:linux防御xorddos 编辑:程序博客网 时间:2024/06/10 02:23
  1. // 初始化输入框并设置位置和大小  
  2. UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(1010300180)];  
  3. // 设置预设文本  
  4. textView.text = @"";  
  5. // 设置文本字体  
  6. textView.font = [UIFont fontWithName:@"Arial" size:16.5f];  
  7. // 设置文本颜色  
  8. textView.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1.0f];  
  9. // 设置文本框背景颜色  
  10. textView.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];  
  11. // 设置文本对齐方式  
  12. textView.textAlignment = NSTextAlignmentLeft;  
  13.   
  14. // iOS7中文本对齐方式有以下几种:  
  15. //    enum {  
  16. //        NSTextAlignmentLeft      = 0,  左对齐,默认  
  17. //        NSTextAlignmentCenter    = 1,  居中对齐  
  18. //        NSTextAlignmentRight     = 2,  右对齐  
  19. //        NSTextAlignmentJustified = 3,  在一个段落的最后一行自然对齐  
  20. //        NSTextAlignmentNatural   = 4,  默认对齐方式  
  21. //    } NSTextAlignment;  
  22.   
  23. // 设置自动纠错方式  
  24. textView.autocorrectionType = UITextAutocorrectionTypeNo;  
  25.   
  26. // 自动纠错方式有以下几种:  
  27. //    enum {  
  28. //        UITextAutocorrectionTypeDefault,  默认  
  29. //        UITextAutocorrectionTypeNo,       不自动纠错  
  30. //        UITextAutocorrectionTypeYes,      自动纠错  
  31. //    } UITextAutocorrectionType;  
  32.   
  33. // 设置自动大写方式  
  34. textView.autocapitalizationType = UITextAutocapitalizationTypeNone;  
  35.   
  36. // 自动大写方式有以下几种:  
  37. //    enum {  
  38. //        UITextAutocapitalizationTypeNone,           不自动大写  
  39. //        UITextAutocapitalizationTypeWords,          单词首字母大写  
  40. //        UITextAutocapitalizationTypeSentences,      句子的首字母大写  
  41. //        UITextAutocapitalizationTypeAllCharacters,  所有字母都大写  
  42. //    } UITextAutocapitalizationType;  
  43.   
  44. // 设置键盘的样式  
  45. textView.keyboardType = UIKeyboardTypeDefault;  
  46.   
  47. // 键盘样式有以下几种:  
  48. //    enum {  
  49. //        UIKeyboardTypeDefault,                默认键盘,支持所有字符  
  50. //        UIKeyboardTypeASCIICapable,           支持ASCII的默认键盘  
  51. //        UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符  
  52. //        UIKeyboardTypeURL,                    只支持URL字符的URL键盘,支持.com按钮  
  53. //        UIKeyboardTypeNumberPad,              数字键盘  
  54. //        UIKeyboardTypePhonePad,               电话键盘  
  55. //        UIKeyboardTypeNamePhonePad,           支持输入人名的电话键盘  
  56. //        UIKeyboardTypeEmailAddress,           电子邮件键盘  
  57. //        UIKeyboardTypeDecimalPad,             有数字和小数点的数字键盘  
  58. //        UIKeyboardTypeTwitter,                优化的键盘,方便输入@、#字符  
  59. //        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,  
  60. //    } UIKeyboardType;  
  61.   
  62. // 设置return键样式  
  63. textView.returnKeyType = UIReturnKeyDefault;  
  64.   
  65. // return键有以下几种样式:  
  66. //    enum {  
  67. //        UIReturnKeyDefault,        默认,灰色按钮,标有Return  
  68. //        UIReturnKeyGo,             标有Go的蓝色按钮  
  69. //        UIReturnKeyGoogle,         标有Google的蓝色按钮,用于搜索  
  70. //        UIReturnKeyJoin,           标有Join的蓝色按钮  
  71. //        UIReturnKeyNext,           标有Next的蓝色按钮  
  72. //        UIReturnKeyRoute,          标有Route的蓝色按钮  
  73. //        UIReturnKeySearch,         标有Search的蓝色按钮  
  74. //        UIReturnKeySend,           标有Send的蓝色按钮  
  75. //        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮  
  76. //        UIReturnKeyYahoo,          标有Yahoo的蓝色按钮  
  77. //        UIReturnKeyEmergencyCall,  紧急呼叫按钮  
  78. //    } UIReturnKeyType;  
  79.   
  80. // 设置是否可以拖动  
  81. textView.scrollEnabled = YES;  
  82. // 设置代理  
  83. textView.delegate = self;  
  84.   
  85. // 自定义文本框placeholder  
  86. tip = [[UILabel alloc] initWithFrame:CGRectMake(161432025)];  
  87. tip.text = @"您的意见是我们前进的最大动力,谢谢!";  
  88. tip.font = [UIFont fontWithName:@"Arial" size:16.5f];  
  89. tip.backgroundColor = [UIColor clearColor];  
  90. tip.enabled = NO;  
  91.   
  92. // 自定义文本框字数统计  
  93. count = [[UILabel alloc] initWithFrame:CGRectMake(2701703520)];  
  94. count.text = @"240";  
  95. count.textAlignment = NSTextAlignmentRight;  
  96. count.font = [UIFont fontWithName:@"Arial" size:15.0f];  
  97. count.backgroundColor = [UIColor clearColor];  
  98. count.enabled = NO;  
  99.   
  100. // 显示文本框及相关控件  
  101. [self.view addSubview:feedback];  
  102. [self.view addSubview:tip];  
  103. [self.view addSubview:count];  
  104.   
  105. // 限制输入文本长度  
  106. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
  107. {  
  108.     if (range.location < 240)  
  109.     {  
  110.         return  YES;  
  111.     } else {  
  112.         return NO;  
  113.     }  
  114. }  
  115.   
  116. // 自定义文本框placeholder  
  117. - (void)textViewDidChange:(UITextView *)textView  
  118. {  
  119.     count.text = [NSString stringWithFormat:@"%d"240 - feedback.text.length];  
  120.     if (textView.text.length == 0)  
  121.     {  
  122.         tip.text = @"您的意见是我们前进的最大动力,谢谢!";  
  123.     } else {  
  124.         tip.text = @"";  
  125.     }  
0 0