user1672134
user1672134

Reputation: 35

add button inside a text view

I have a text view in which i want a button for camera. In my application i want that user type some text then he/she want to attach a photo or video. I have done that but the camera button i want inside the text view. when i try to add a button as subview that button scrolled with the text but i want button fixed and another problem is that the text comes in button area not visible so i want that the cursor will not go in that area which is covered by button.

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0, 0, 50.0, 30.0);

[editButton addTarget:self action:@selector(editPressed:)forControlEvents:UIControlEventTouchUpInside];
myButton.backgroundColor=[UIColor blueColor];
[txtView addSubview:myButton];

Upvotes: 0

Views: 3424

Answers (2)

Jayaprada
Jayaprada

Reputation: 954

enter image description here - enter image description here

  • Try this .It will definitely work for you and will solve your issue

  • Write below code in ViewDidLoad Method.

    .

-(void)textResizeForButton{

    CGRect buttonFrame = self.button.frame;
    UIBezierPath *exclusivePath = [UIBezierPath bezierPathWithRect:buttonFrame];
    self.textView.textContainer.exclusionPaths = @[exclusivePath];

}

Upvotes: 5

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

Add it on self.view not on UITextField so it won't be scrolled anywhere. Just get the CGrect right.

For offsetting the text on the right to fit UIButton try this:

    UIView *rightMarginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftMargin, txtField.frame.size.height)];
    rightMarginView.userInteractionEnabled = NO;
    [txtField setRightView:rightMarginView];
    txtField.rightViewMode = UITextFieldViewModeAlways;

Upvotes: 2

Related Questions