2016-03-11 36 views
7

desidero tracciare una linea personalizzabile all'interno di un UITextView costituito da un testo (utilizzando NSAttributedString)Tracciare una linea all'interno di un UITextView - NSAttributedString

Ecco quello che ho cercato

NSString *unicodeStr = [NSString stringWithFormat:@"%C%C%C", 0x00A0, 0x0009, 0x00A0]; //nbsp, tab, nbsp 
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr]; 
NSRange strRange = NSMakeRange(0, str.length); 

NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init]; 
tabStyle.headIndent = 16; //padding on left and right edges 
tabStyle.firstLineHeadIndent = 16; 
tabStyle.tailIndent = -16; 
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:40 options:@{}]; //this is how long I want the line to be 
tabStyle.tabStops = @[listTab]; 
[str addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange]; 
[str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange]; 

Ma non importa quale valore I fornire la posizione di tabulazione (40 in questo caso) e tailIndent (-16 qui), la linea rispetta solo l'headIndent e copre l'intera larghezza di UITextView (meno il valore headIndent, ovviamente).

EDIT - Sono abbastanza sicuro che il problema è perché non sto utilizzando i caratteri Unicode corretti (anche se sembrano essere la scelta logica). Nel caso questo dia a qualcuno un suggerimento, se aggiungo uno spazio dopo il 2 nbsp, cioè verso la fine, la scheda è limitata a una singola lunghezza di tabulazione

+0

Puoi fornire un esempio di come desideri che il testo sia simile? –

+0

Sto tentando di implementare un tag hr (regola orizzontale). Non sono supportati da NSAttributedString durante la conversione da html – lostInTransit

risposta

2

È il tuo risultato previsto?

enter image description here enter image description here

Potete provare questo:

NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.firstLineHeadIndent + tabStyle.tailIndent options:@{}]; 

e questo è il codice completo:

- (void) viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    NSString *unicodeStr = @"\n\u00a0\t\t\n"; 
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr]; 
    NSRange strRange = NSMakeRange(0, str.length); 

    NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init]; 
    tabStyle.headIndent = 16; //padding on left and right edges 
    tabStyle.firstLineHeadIndent = 16; 
    tabStyle.tailIndent = -70; 
    NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.headIndent + tabStyle.tailIndent options:@{}]; //this is how long I want the line to be 
    tabStyle.tabStops = @[listTab]; 
    [str addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange]; 
    [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange]; 

    NSAttributedString *htmlStr = [[NSAttributedString alloc] initWithData:[@"<h1>Lorem ipsum dolor sit er elit lamet</h1>" dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 

    [str insertAttributedString:htmlStr atIndex:0]; 
    [str appendAttributedString:htmlStr]; 

    self.textView.attributedText = str; 
} 
+0

Già, non ha funzionato. Sono abbastanza sicuro che abbia qualcosa a che fare con il nbsp su entrambi i lati della scheda, ma non riesco a capire perché – lostInTransit

+0

è questo che voglio - sì. Ma ho un testo prima e dopo (html) e questo sembra rovinare la regola orizzontale (credo?). Il codice non sembra essere molto diverso da quello che ho. Non sono sicuro del motivo per cui non funzionerebbe per me. – lostInTransit

+0

che sembrava funzionare! Grazie – lostInTransit

0

Ecco quello che ho fatto per risolvere lo stesso problema. Esso utilizza una sottoclasse di NSTextTab:

import UIKit 

class RightAnchoredTextTab : NSTextTab { 
    weak var textContainer : NSTextContainer! 

    required init(textAlignment alignment: NSTextAlignment, location loc: CGFloat, options: [String : AnyObject], textContainer aTextContainer : NSTextContainer) { 
     super.init(textAlignment: alignment, location: loc, options: options) 
     textContainer = aTextContainer 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override var location: CGFloat { 
     get { 
      return textContainer.size.width-textContainer.lineFragmentPadding*2-super.location 
     } 
    } 
} 

e simili bit simile ad altre soluzioni:

func horizontalLine(indent : CGFloat = 30, width : CGFloat = 1) -> NSAttributedString 
{ 
    let paragraphStyle = NSMutableParagraphStyle() 

    paragraphStyle.tabStops = [] 
    paragraphStyle.addTabStop(NSTextTab(textAlignment: .Left, location: indent, options: [:])) 
    paragraphStyle.addTabStop(RightAnchoredTextTab(textAlignment: .Right, location: indent, options: [:], textContainer : textView.textContainer)) 
    paragraphStyle.alignment = .Left 

    let attributes = [NSParagraphStyleAttributeName : paragraphStyle, NSStrikethroughStyleAttributeName : width] 

    textView.backgroundColor = UIColor.yellowColor() 
    let ZeroWidthNonBreakingSpace = "\u{FEFF}" 
    return NSAttributedString(string: "\t\(ZeroWidthNonBreakingSpace)\t\(ZeroWidthNonBreakingSpace)\n", attributes: attributes) 
} 

Alcune note:

  • Sarà probabilmente funziona solo con un singolo contenitore di testo quadrato .
  • NSTextContainer ha un lineFragmentPadding. Se vi siete mai chiesti perché è necessario indentare una tabulazione di allineamento a destra di 10, questo è il seguente: il valore predefinito è 5.
  • che precede il testo con \n significa che non è stato barrato. Non ho idea del perché.
  • Questa soluzione funziona con le rotazioni ecc. Come UITextView chiamate di nuovo NSTextTab.location quando i limiti vengono modificati.