ravi
ravi

Reputation: 120

Create a UIbutton and UIImageview in UITextView

I have a UITextview in my Screen,I created textview in drag and drop on screen, now i want to add button to last (end of the text in text view )of the textview then how can i do that ??

i have been try

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

}

but that not work ..

Upvotes: 0

Views: 5655

Answers (6)

Rama Rao
Rama Rao

Reputation: 1033

enter image description here

    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.

Upvotes: 4

gauravds
gauravds

Reputation: 3011

It will work.

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

Upvotes: 1

mChopsey
mChopsey

Reputation: 548

Try this code. Here iam creating textview also. This worked fine for me.

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

Upvotes: 1

gauravds
gauravds

Reputation: 3011

Check it out dear, it works for me always.

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

Upvotes: 1

mChopsey
mChopsey

Reputation: 548

Iam not sure whether you are using Interface builder.

U can try this

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

Upvotes: 3

iNoob
iNoob

Reputation: 3364

I'm guessing you should mention the type of button while you are adding through code. I just got it working with this:

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

Upvotes: 2

Related Questions