0

我的屏幕上有一个 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];

}

但这不起作用..

4

6 回答 6

4

在此处输入图像描述

    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.
于 2012-04-11T11:07:45.547 回答
3

我不确定您是否正在使用界面生成器。

你可以试试这个

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0.0, 100.0, 50.0, 30.0);
[yourTextView addSubview:myButton];
于 2012-04-11T11:05:32.210 回答
2

我猜你应该在type通过代码添加时提到 of 按钮。我刚刚得到它的工作:

UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"test" forState:UIControlStateNormal];
[yourTextView addSubview:button];
于 2012-04-11T11:04:36.833 回答
1

亲爱的,检查一下,它总是对我有用。

for (id view in [textView subviews]) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,35)];
            [view addSubview:btn];
        }
    }
于 2012-04-11T11:27:53.880 回答
1

试试这个代码。这里我也在创建 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];
于 2012-04-11T11:33:40.120 回答
1

它会起作用的。

 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];
            }
        }
于 2012-04-11T12:05:01.583 回答