2010-07-12 15 views
42

Ho un UITextView per la modifica del testo. Per impostazione predefinita, ha un piccolo margine attorno al testo. Voglio aumentare tale margine di alcuni pixel.Come impostare i margini (riempimento) in UITextView?

La proprietà contentInset mi dà margini, ma non modifica il testo "larghezza di avvolgimento". Il testo è avvolto alla stessa larghezza e il "margine" extra fa semplicemente scorrere la vista orizzontalmente.

C'è un modo per rendere una UITextView di una certa larghezza visualizzare il testo con una "larghezza di avvolgimento" più stretta?

risposta

7

Si potrebbe semplicemente utilizzare un UITextView più piccolo e posizionare un UIView in background per simulare il padding.

+----------+ 
|   | <-- UIView (as background) 
| +--+ | 
| | | <---- UITextView 
| | | | 
| +--+ | 
|   | 
+----------+ 
+1

Sì, questo è quello che sto facendo ora. Funziona bene per i margini laterali, ma il margine superiore ha problemi. Dal punto di vista dell'utente, il testo viene "troncato" nella parte superiore durante lo scorrimento perché il testo viene visualizzato solo in UITextView. – strawtarget

+5

E non è possibile visualizzare correttamente la barra di scorrimento verticale in questo modo. – an0

+0

L'impostazione 'textView.clipsToBounds = NO'" riparerà "il taglio in alto, una specie di. Ovviamente, questo non aiuta a mostrare correttamente la barra di scorrimento verticale. – villapossu

1

Questo funziona per me. Cambiare il testo Visualizza contenutoInset e cornice inserto/posizione per ottenere il padding e il ritorno a capo corretto. Quindi modificare i limiti del testo per impedire lo scorrimento orizzontale. È inoltre necessario reimpostare il padding ogni volta che il testo viene selezionato e modificato.

- (void)setPadding 
{ 
    UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50); 

    textView.contentInset = padding; 

    CGRect frame = textView.frame; 

    // must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets 
    CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right)); 

    // offset frame back to original x 
    CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - (padding.left + padding.right)/2); 
    insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0)); 

    textView.frame = insetFrame; 

    textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right)); 

    [textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO]; 
} 

-(void)textViewDidChangeSelection:(UITextView *)textView 
{ 
    [self setPadding]; 
} 

-(void)textViewDidChange:(UITextView *)textView 
{ 
    [self setPadding]; 
} 
+0

@Nate questo non funziona per me. La vista testo continua a scorrere orizzontalmente e il riquadro contenuto si esaurisce all'esterno della cornice della vista testo – GoGreen

34

Dopo trafficando con questo per un po 'ho trovato un'altra soluzione se si sta sviluppando solo per iOS 6. Impostare i margini superiore e inferiore con contentInset:

textView = [[UITextView alloc] init]; 
textView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0); 

Per la sinistra e i margini a destra non aggiungono il testo normale ma utilizzano una stringa NSAttributed invece di impostare correttamente i rientri sinistro e destro con uno NSMutableParagraphStyle:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.headIndent = 20.0; 
paragraphStyle.firstLineHeadIndent = 20.0; 
paragraphStyle.tailIndent = -20.0; 

NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont fontWithName:@"TrebuchetMS" size:12.0], NSParagraphStyleAttributeName: paragraphStyle}; 
textView.attributedText = [[NSAttributedString alloc] initWithString:myText attributes:attrsDictionary]; 

Questo vi dà un UITextView con il testo (nel mio caso dalla variabile myText) con 20 pixel riempimento che scorre in modo corretto.

89

A partire da iOS 7 è possibile utilizzare textContainerInset proprietà:

Objective-C

textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 20); 

Swift

textView.textContainerInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20) 
8

Prova questo

UIBezierPath* aObjBezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 20)]; 
txtView.textContainer.exclusionPaths = @[aObjBezierPath]; 
0

Grazie per i suggerimenti. Ho avuto questo problema quando si sviluppa un'applicazione MacOS/OSX e finito con questo:

textView.textContainerInset = NSSize(width: 20, height: 20); //Sets margins 
0

Prova sottostante Codice

Per iOS7 utilizzare textContainerInset

textView.textContainerInset = UIEdgeInsetsMake (0, 20, 0, 20);

controllano sotto il collegamento potrebbe essere utile a voi

https://stackoverflow.com/a/22935404/5184217

Problemi correlati