NSTextContainer、NSLayoutManager、NSTextStorage
è nuovo a iOS7:
1) NSTextContainer:
classe Il NSTextContainer definisce una regione in cui il testo è disposto. Un oggetto NSTextContainer definisce le regioni rettangolari ed è possibile definire i percorsi di esclusione all'interno dei rettangoli di testo contenenti i trattini di testo in cui si trova l'esclusione.
2) NSLayoutManager:
Un NSLayoutManager oggetto coordina il layout e la visualizzazione dei caratteri depositate su un oggetto NSTextStorage. Mappa i codici carattere Unicode ai glifi, imposta i glifi in una serie di oggetti NSTextContainer e li visualizza in una serie di oggetti vista testo.
3) NSTextStorage:
NSTextStorage è una sottoclasse di semiconcrete NSMutableAttributedString che gestisce una serie di NSLayoutManagerobjects client, notifyingthemofanychangestoitscharactersorattributessothattheycanrelay e visualizzare nuovamente il testo in base alle esigenze.
Potremmo sappiamo NSTextStorage
in grado di memorizzare e gestire UITextView
's del testo, ed è NSMutableAttributedString
' s subclass.We può aggiungere o modificare gli attributi, quindi è una buona scelta per memorizzare e gestire UITextView
's testo.
NSLayoutManager
utilizzare per gestire il contenuto del layout di NSTextStorage
.
NSTextContainer
fornire un rettangolo per il testo impaginato.
Possiamo semplicemente utilizzare:
CGRect textViewRect = CGRectInset(self.view.bounds, 10.0, 20.0);
// NSTextContainer
NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(textViewRect.size.width, CGFLOAT_MAX)]; // new in iOS 7.0
container.widthTracksTextView = YES; // Controls whether the receiveradjusts the width of its bounding rectangle when its text view is resized
// NSLayoutManager
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; // new in iOS 7.0
[layoutManager addTextContainer:container];
// NSTextStorage subclass
self.textStorage = [[TextStorage alloc] init]; // new in iOS 7.0
[self.textStorage addLayoutManager:layoutManager];
viene dapprima rendono istanza di loro, e crea bontà relationship.You deve aggiungere NSTextContainer
in UITextView
da initWithFrame:textContainer:
metodo.
// UITextView
UITextView *newTextView = [[UITextView alloc] initWithFrame:textViewRect textContainer:container];
newTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
newTextView.scrollEnabled = YES;
newTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
// newTextView.editable = NO;
newTextView.font = [UIFont fontWithName:self.textStorage.fontName size:18.0];
newTextView.dataDetectorTypes = UIDataDetectorTypeAll;
self.textView = newTextView;
[self.view addSubview:self.textView];
Se desidera utilizzare UITextStorage
per modificare gli attributi di testo, è possibile utilizzare:
[_textStorage beginEditing]; // begin edit
[_textStorage endEditing]; // end edit
Tra di loro è possibile modificare il testo, come ad esempio:
[_textStorage beginEditing];
NSDictionary *attrsDic = @{NSTextEffectAttributeName: NSTextEffectLetterpressStyle};
UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0); // NSString, default nil: no text effect
NSMutableAttributedString *mutableAttrString = [[NSMutableAttributedString alloc] initWithString:@"Letterpress" attributes:attrsDic];
NSAttributedString *appendAttrString = [[NSAttributedString alloc] initWithString:@" Append:Letterpress"];
[mutableAttrString appendAttributedString:appendAttrString];
[_textStorage setAttributedString:mutableAttrString];
[_textStorage endEditing];
o cambiare colore:
[_textStorage beginEditing];
/* Dynamic Coloring Text */
self.textStorage.bookItem = [[BookItem alloc] initWithBookName:@"Dynamic Coloring.rtf"];
self.textStorage.tokens = @{@"Alice": @{NSForegroundColorAttributeName: [UIColor redColor]},
@"Rabbit": @{NSForegroundColorAttributeName: [UIColor greenColor]},
DefaultTokenName: @{NSForegroundColorAttributeName: [UIColor blackColor]}
};
[_textStorage setAttributedString:_textStorage.bookItem.content];
[_textStorage endEditing];
Hai trovato altre informazioni su questo? – dannyzlo
@DannyZlobinsky Non proprio. C'è stata una risposta qui da utente di alta reputazione ma è stata cancellata (?!). Sto ancora aspettando alcune buone risposte. – drasto
Ecco un buon tutorial video in cui vengono illustrati i principi di NSTextStorage. Spero che possa aiutarti qualcosa che è lì ma sembra interessante: https://www.youtube.com/watch?v=y7trOFDGVwA – juanmajmjr