5

I have a UITextView and it has a weird margin at the top, not sure what's causing this. Here's the picture, the background is orange:

enter image description here

Here's my relevant code:

textViewTest = [[UITextView alloc] initWithFrame:CGRectMake(135, 0, 150, 68)];
[textViewTest setContentInset:UIEdgeInsetsZero];
[textViewTest setUserInteractionEnabled:NO];
[textViewTest setBackgroundColor:[UIColor orangeColor]];
[textViewTest setTextColor:[UIColor whiteColor]];
//[textViewTest setFont:[UIFont fontWithName:@"MuseoSans-500" size:12.0]];
[textViewTest setText:@"Spooky (rename)\nCreated: 4/10/11\nUpload Youtube\nDelete | Favorite"];

What I want is the text in UITextView (textViewTest) to not have any space from the top (margin). Currently, there's like 8-10 px from the top of the orange, then the text starts.

0xSina
  • 20,973
  • 34
  • 136
  • 253

4 Answers4

19

If you only want to move the text, try

[textViewTest setContentInset:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];

Where a positive number moves the frame towards the middle, a negative moves it out from the middle.

For example, [textViewTest setContentInset:UIEdgeInsetsMake(-5, 0, 5,0)], will move the text 5 pixels up!

patridge
  • 26,385
  • 18
  • 89
  • 135
Karl Eriksson
  • 431
  • 3
  • 5
  • This solution is good, but why does it happen? For some reason on iOS7 the controls has margin of about 40 pixels for no reason. – bashan Nov 20 '13 at 20:20
  • @bashan for the 40pixel margin, you need to do this: http://stackoverflow.com/questions/18931934/blank-space-at-top-of-uitextview-in-ios-7 – strange May 14 '14 at 02:14
  • Thanks. I solved it this way. I was just wondering why is that and why is it so hard to control... – bashan May 16 '14 at 18:46
2

In viewDidLoad add:

 self.automaticallyAdjustsScrollViewInsets = NO;
Hsm
  • 1,510
  • 17
  • 16
-1

Try sizing your text view to fit its text: [textViewTest sizeToFit];

Snowman
  • 31,411
  • 46
  • 180
  • 303
-1

I think you want to align the orange color view same as the red color view. what is the red color view frame? I think it is the problem of the frame.

why not using the red color view y like

textViewTest = [[UITextView alloc] initWithFrame:CGRectMake(135, 
                                                            [redView frame].origin.y,
                                                            150, 68)];
Lunayo
  • 538
  • 7
  • 32
  • What I want is the text in UITextView (textViewTest) to not have any space from the top (margin). Currently, there's like 8-10 px from the top of the orange, then the text starts. – 0xSina Mar 18 '12 at 19:32
  • oh! I guess what you want is vertical alignment. and there is no easy way vertical text alignment in UITextView check this link http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/. Or you can change it to an UITextField and add it to a view. And set the `contentVerticalAlignment` to `UIControlContentVerticalAlignmentCenter` – Lunayo Mar 18 '12 at 19:42