iOS開發系列之四 - UITextView 用法小結

來源:互聯網
上載者:User

iOS開發系列之四 - UITextView 用法小結

// 初始化輸入框並設定位置和大小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鍵盤,支援.com按鈕//        UIKeyboardTypeNumberPad,              數字鍵台//        UIKeyboardTypePhonePad,               電話鍵盤//        UIKeyboardTypeNamePhonePad,           支援輸入人名的電話鍵盤//        UIKeyboardTypeEmailAddress,           電子郵件鍵盤//        UIKeyboardTypeDecimalPad,             有數字和小數點的數字鍵台//        UIKeyboardTypeTwitter,                最佳化的鍵盤,方便輸入@、#字元//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,//    } UIKeyboardType;// 設定return鍵樣式textView.returnKeyType = UIReturnKeyDefault;// return鍵有以下幾種樣式://    enum {//        UIReturnKeyDefault,        預設,灰色按鈕,標有Return//        UIReturnKeyGo,             標有Go的藍色按鈕//        UIReturnKeyGoogle,         標有Google的藍色按鈕,用於搜尋//        UIReturnKeyJoin,           標有Join的藍色按鈕//        UIReturnKeyNext,           標有Next的藍色按鈕//        UIReturnKeyRoute,          標有Route的藍色按鈕//        UIReturnKeySearch,         標有Search的藍色按鈕//        UIReturnKeySend,           標有Send的藍色按鈕//        UIReturnKeyYahoo,          標有Yahoo的藍色按鈕//        UIReturnKeyYahoo,          標有Yahoo的藍色按鈕//        UIReturnKeyEmergencyCall,  緊急電話按鈕//    } UIReturnKeyType;// 設定是否可以拖動textView.scrollEnabled = YES;// 設定代理textView.delegate = self;// 自訂文字框placeholdertip = [[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 = @"";    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.