使用UILabels创建自定义UITextView以及其中的文本 [英] Creating custom UITextView with UILabels as well as text in it

查看:131
本文介绍了使用UILabels创建自定义UITextView以及其中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义 UITextView ,可以在其中输入文本,以及以编程方式添加一些 UILabels 那里的行为就像文本(当光标靠近它们时,我需要用'backspace'按钮删除它们)。



这个 UITextView 应该是可展开的,标签可以有不同的宽度。



任何想法如何创建这种东西,任何教程或



<$ p

您可以使用此代码创建文本字段。


解决方案

$ p> UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(10,200,300,40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @输入文字;
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(@退格按钮按下);
[label removeFromSuperview];
}
return YES;
}

如果按下退格按钮,replaceString(string)值。所以我们可以使用这个标识退格按钮。


I would like to create a custom UITextView with the ability to enter text in it, as well as programmatically adding some UILabels there that would act like text (I need to delete them with the 'backspace' button when the cursor comes near them).

This UITextView should be expandable and the labels can have different width.

Any ideas of how you can create this sort of thing, any tutorials or such?

解决方案

you can create textfield using this code.

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];

And for creating label you can use this code:

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];

and for deleting label when backspace tape use this method:

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

If the backspace button is pressed, then the replacementString(string) will have null value. So we can identify backspace button press using this.

这篇关于使用UILabels创建自定义UITextView以及其中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆