2013-04-03 10 views
5

Come aumentare la spaziatura tra le righe in UITextView?Come aumentare la spaziatura tra le righe in UITextView

desidero utilizzare font di sistema di default, vale a dire, Helvetica con dimensioni 15.

+0

possibile duplicato di [Come controllare l'interlinea in UILabel] (http://stackoverflow.com/questions/5494498/how-to-control-the-line-spacing-in-uilabel) – Sulthan

+0

http: // stackoverflow.com/questions/4277551/iphone-uitextview-set-line-spacing –

+0

http://stackoverflow.com/questions/3760924/set-line-height-in-uitextview –

risposta

4

In IOS6 + è possibile impostare gli attributi di tipizzazione per un UITextView

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle setLineSpacing:lineSpacing]; 

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; 

[textView setTypingAttributes:attrsDictionary]; 
8

Dichiarare si implementa il protocollo con l'aggiunta di

<NSLayoutManagerDelegate>

all'interfaccia. Quindi, impostare:

yourTextView.layoutManager.delegate = self;

Poi l'override di questo metodo delegato:

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect 
{ 
    return 5; // Line spacing of 19 is roughly equivalent to 5 here. 
} 

UPDATE: Recentemente ho scoperto che questo può anche essere fatto utilizzando il NSMutableParagraphStyle setLineSpacing: API.

Nel tentativo di fornire sempre un utile codice copia-snippet, ecco qua!

NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init]; 
[myStyle setLineSpacing:myLineSpacingInt]; 
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange]; 
[myViewElement setAttributedText:myString]; 

^myViewElement può essere un UITextField, UILabel o UITextView.

Problemi correlati