繁体   English   中英

在UITextView中创建一个UIbutton和UIImageview

[英]Create a UIbutton and UIImageview in UITextView

我的屏幕上有一个UITextview,我在屏幕上的拖放操作中创建了textview,现在我想在textview的最后(文本视图中的文本结尾)添加按钮,然后该怎么做?

我一直在尝试

[super viewDidLoad];
 SelectInvestiList = [[NSMutableArray alloc] initWithCapacity:[InvestiList count]]; 


AppDelegate *appDeleg = (AppDelegate *)[[UIApplication sharedApplication]delegate];

textViewInvest.text=nil;

UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"test" forState:UIControlStateNormal];
[textViewInvest addSubview:button];

NSMutableString *prev=[[NSMutableString alloc] init];
if (appDeleg.chiefCompText != nil) {
    prev=(NSMutableString *) textViewInvest.text;
    [prev appendString:(NSMutableString *) appDeleg.chiefCompText];
    textViewInvest.text=[prev capitalizedString];

}

if (appDeleg.vitalText != nil) {
    prev=(NSMutableString *)textViewInvest.text;
    [prev appendString:(NSMutableString *) appDeleg.vitalText];
    textViewInvest.text=[prev capitalizedString];

}

但这不起作用..

在此处输入图片说明

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setFrame:CGRectMake(0, 0, 10, 20)];
        [btn setTitle:@"Ram" forState:UIControlStateNormal];
        //[self.view addSubview:btn];
        [textview addSubview:btn];
set the button frame according to your text in textview.

我不确定您是否正在使用“界面构建器”。

你可以试试看

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0.0, 100.0, 50.0, 30.0);
[yourTextView addSubview:myButton];

我猜您在通过代码添加时应该提到按钮的type 我只是用它来工作:

UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"test" forState:UIControlStateNormal];
[yourTextView addSubview:button];

看看吧,亲爱的,它总是对我有用。

for (id view in [textView subviews]) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,35)];
            [view addSubview:btn];
        }
    }

试试这个代码。 在这里我也创建textview。 这对我来说很好。

UITextView *yourTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 400.0)];
yourTextView.backgroundColor    =   [UIColor grayColor];
[self.view addSubview:yourTextView];

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0.0, 100.0, 50.0, 30.0);
[myButton setTitle:@"GO" forState:UIControlStateNormal];
[yourTextView addSubview:myButton];

它会工作。

 for (id view in [textView subviews]) {
            if ([view isKindOfClass:[UIScrollView class]]) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
                [btn setFrame:CGRectMake(0, 0, 10, 20)]; 
                [btn setTitle:@"Ram" forState:UIControlStateNormal];

                [view addSubview:btn];
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM