0

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 ..

ravi
  • 120
  • 1
  • 10
  • It looks that you didn't properly _connect_ `IBOutlet` of view's owner (property `textViewInvest`) to your `UITextView`. – Rok Jarc Apr 11 '12 at 11:58
  • no i connect well bcoz i put text in it from web Services – ravi Apr 11 '12 at 12:00
  • http://stackoverflow.com/questions/2719958/how-to-use-http-live-streaming-protocol-in-iphone-sdk-3-0 u r invited to this chat pls check – Rama Rao Aug 04 '12 at 10:08

6 Answers6

4

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.
gauravds
  • 2,931
  • 2
  • 28
  • 45
Rama Rao
  • 1,043
  • 5
  • 22
  • no.. i has tried your code but button is not show in my textview – ravi Apr 11 '12 at 11:28
  • Its working in my app.I checked three to four times.U allocate textview with frame properly.I added textview to the screen by code. – Rama Rao Apr 11 '12 at 11:32
  • @S Rama Rao if i also added textview to screen by code than it's work but i need to dread and drop textview.. so that not show up button – ravi Apr 11 '12 at 11:37
  • ya.. but button put programicaly – ravi Apr 11 '12 at 11:39
  • check this edited answer that is added on my app when textview is added in interface builder and button is added to textview programettically – Rama Rao Apr 11 '12 at 11:46
  • @S Rama can u tell me how can i get size of end of textview text , the text is generated dynemically meance not fix – – ravi Apr 11 '12 at 11:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9956/discussion-between-s-rama-rao-and-ravi) – Rama Rao Apr 11 '12 at 11:59
  • is that button included in scrolling view. means when u write many text into textview at that time button will not be the part of scrollview I hope so – gauravds Apr 11 '12 at 13:16
3

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];
mChopsey
  • 548
  • 3
  • 10
2

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];
iNoob
  • 3,364
  • 2
  • 26
  • 33
  • no.. i has tried your code but button is not show in my textview – ravi Apr 11 '12 at 11:27
  • @ravi, that's unusual, i tested it just before posting those lines. I hope you are putting these lines of code in `viewDidLoad` ? – iNoob Apr 11 '12 at 11:31
  • @ravi, yes just went through it, is the `textViewInvest` behaving like it should ? Does changing the .text of textView change the text ? Only thing i can think of is that `textViewInvest` is not connected to the IB. And sometimes using code before `[super viewDidLoad];` helps. Just check if it's connected properly. – iNoob Apr 11 '12 at 11:46
1

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];
        }
    }
gauravds
  • 2,931
  • 2
  • 28
  • 45
  • what is id here ?? and you put button in the UIScrollView ?? – ravi Apr 11 '12 at 11:30
  • how much programming experience do u have in iOS development. paste this code in ur project it will work dear – gauravds Apr 11 '12 at 11:32
  • around 5 month no dear that's not Work – ravi Apr 11 '12 at 11:46
  • id is a datatype which is able to hold any type of object, more detail google it – gauravds Apr 11 '12 at 11:47
  • I tested it for UIImage and it always work. U should make proper button and then add it. it will work. I bet u. – gauravds Apr 11 '12 at 11:53
  • for (id view in [textView subviews]) {        if ([view isKindOfClass:[UIScrollView class]]) {                        [view addSubview:[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"noimage.png"]]];        }    } – gauravds Apr 11 '12 at 11:54
  • can u tell me how can i get size of end of textview text , the text is generated dynemically meance not fix – ravi Apr 11 '12 at 11:56
  • - (void)textViewDidBeginEditing:(UITextView *)textView{ NSInteger i= [textView.text length]; } – gauravds Apr 11 '12 at 11:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9957/discussion-between-gaurav-d-sharma-and-ravi) – gauravds Apr 11 '12 at 12:00
1

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];
mChopsey
  • 548
  • 3
  • 10
1

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];
            }
        }
gauravds
  • 2,931
  • 2
  • 28
  • 45