簡體   English   中英

使用UILabels創建自定義UITextView以及其中的文本

[英]Creating custom UITextView with UILabels as well as text in it

我想創建一個自定義UITextView ,能夠在其中輸入文本,以及以編程方式在那里添加一些像文本一樣的UILabels (當光標靠近它時我需要用'退格'按鈕刪除它們)。

UITextView應該是可擴展的,並且標簽可以具有不同的寬度。

關於如何創建這類東西,任何教程等的任何想法?

您可以使用此代碼創建文本字段。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
textField.delegate = self;
[self.view addSubview:textField];
[textField release];

要創建標簽,您可以使用以下代碼:

CGRect labelFrame = CGRectMake( 10, 40, 100, 30 );
    UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
    [label setText: @"My Label"];
    [label setTextColor: [UIColor orangeColor]];
    label.backgroundColor =[UIColor clearColor];
    [view addSubview: label];

當退格磁帶使用此方法時刪除標簽:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@""]) {
NSLog(@"backspace button pressed");
[label removeFromSuperview];
}
return YES;
}

如果按下退格按鈕,則replacementString(string)將具有null值。 因此我們可以使用此功能識別退格按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM