Dodaj "padding" do UITextView

Jak mówi tytuł, próbuję dodać zachowanie podobne do wypełnienia do UITextView. Widok tekstowy jest generowany, gdy widok jest wciśnięty do kontrolera nawigacji i występuje następujący kod:

 self.textView.layer.cornerRadius = 7;
 //pretty stuff is pretty

 NSString *description = appDelegate.productInDisplay.description;
 [self.textView setText:description];
 //pretty stuff has content now


 CGRect frame = textView.frame;
 frame.size.height = textView.contentSize.height;
 textView.frame = frame;
 //set the UITextView to the size of it's containing text.

 self.textView.editable = NO;

  self.theScrollView.contentSize = CGSizeMake(320, 250 + textView.frame.size.height);
  //set the parent scrollView's height to fit some other elements with fixed size (250)
  //and the pre-mentioned UITextView

Więc, to wszystko działa i jest ok, ale chcę dodać trochę padding na wszystkich 4 stronach UITextView i nie byłem w stanie tego zrobić z 3 godzin googlowania dla czegoś, co wydaje się raczej łatwe. Jakieś sugestie?

Author: magtak, 2011-10-26

5 answers

EDIT 1/16/2015: to zostało napisane w 2012 roku i nie jest już dokładne. Use-textContainerInset jak wspomniano powyżej.

Original post:

Używanie contentInset nie zadziała. Masz dwa rozsądne opcje: podklasę UITextField i nadpisanie metod textRectForBounds: i editingrectforbounds: lub utwórz swoje pole tekstowe przejrzyście nad UIView i styl UIView.

Przykład podklasowania pola UITextField:

- (CGRect)textRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
     return CGRectInset(bounds, 5, 5);
}

Przykład owijanie w UIView:

UIView *wrapView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 30)];
[wrapView addSubview:textView];
wrapView.layer.borderColor = [UIColor darkGrayColor].CGColor;
wrapView.layer.borderWidth = 2.0;
wrapView.layer.cornerRadius = 5.0;
 24
Author: Jamon Holmgren,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-17 05:07:40

Używając iOS7 zrobiłem to tylko z

[self.textView setTextContainerInset:UIEdgeInsetsMake(0, 12, 0, 12)];

Mam nadzieję, że to pomoże, Mário

 72
Author: Mário Carvalho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-29 20:15:37

Ta odpowiedź jest całkowicie błędna. Poprawna odpowiedź to po prostu:

uitextview.textContainerInset =
       UIEdgeInsetsMake(8,5,8,5); // top, left, bottom, right

(wartości te zazwyczaj odpowiadają wyglądowi pola UITextField na tym samym ekranie.)


Użyj tego:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5); 
 42
Author: Sergej Metelin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-12 13:51:28

To działało u mnie przy użyciu swift 2.0:

textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10)

 6
Author: Julian B.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-14 01:06:53

Dla Swift 3-odpowiedź wydaje się podobna, ale nieco inna:

textview.contentEdgeInsets = UIEdgeInsets(top: 2, left: 10, bottom: 2, right: 10)

(właściwie, użyłem powyższego dla UIButtonView, ale ponieważ jest tak zamknięty dla odpowiedzi, które zostały wysłane do UITextView, myślę [mam nadzieję], że dotyczy to również)

 1
Author: Adam Stoller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-11 15:10:48