2015-01-22 10 views
7

Nella mia applicazione ho bisogno di impostare Leggi di più nella vista del testo se l'input di testo è grande.so il mio approccio è quello di trovare l'intervallo di stringa che si adatterebbe nella visualizzazione del testo e aggiungere Altre informazioni ad esso. C'è un modo per realizzarlo in Swift. L'esigenza è di emulare l'opzione di lettura più per mostrare il testo completo in vista dettagliata su tap come su facebook.Trova il numero di caratteri che si adatta a UITextView con larghezza e altezza costanti con una determinata stringa?

risposta

6

La funzione che si sta cercando è CTFramesetterSuggestFrameSizeWithConstraints. In sostanza, ti consente di scoprire il numero di caratteri che si adattano a un determinato frame. È possibile utilizzare quel numero per tagliare il testo corrente e inserire un pulsante.

ho scritto un'implementazione di questa funzione per una sottoclasse di un UILabel:

- (NSInteger)numberOfCharactersThatFitLabel { 

    // Create an 'CTFramesetterRef' from an attributed string 
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL); 
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:(__bridge id)fontRef forKey:(id)kCTFontAttributeName]; 
    CFRelease(fontRef); 
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:attributes]; 
    CTFramesetterRef frameSetterRef = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString); 

    // Get a suggested character count that would fit the attributed string 
    CFRange characterFitRange; 
    CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0,0), NULL, CGSizeMake(self.bounds.size.width, self.numberOfLines*self.font.lineHeight), &characterFitRange); 
    CFRelease(frameSetterRef); 
    return (NSInteger)characterFitRange.length; 
} 

Here's a bog post per la piena attuazione di tagliare testo casuale di un determinato numero di righe e aggiungendo "visione più" testo ad esso .

+0

Hai osservato che il word wrapping, come si fa a confezionarlo? –

2

È un po 'complicato, perché alcune lettere sono più larghe di altre. Ma è possibile controllare la larghezza della stringa utilizzando l'sizeWithAttributes -Metodo:

var yourString: String = textField.text 
let myString: NSString = originalString as NSString 

//Set your font and add attributes if needed. 
let stringSize: CGSize = myString.sizeWithAttributes([NSFontAttributeName: yourFont]) 

Ora si riceve una CGSize e si può controllare se la larghezza è più ampio di quanto il tuo campo di testo.

if(sizeOfYourTextfield < stringSize){ 
    //String is too large for your UITextField 
} 
+0

sarò in grado di ottenere la gamma di stringa che risiede all'interno del formato specificato? – Sulabh

+0

Sì, ma penso che l'unica possibilità sia quella di scorrere le lettere e controllare se si adattano ancora. – Christian

+0

ho provato questa soluzione, ma a volte non restituisce – Sulabh

2

Una traduzione approssimativa rapida dell'eccellente risposta di kgaidis.

extension UILabel { 

    func numberOfCharactersThatFitLabel() -> Int { 
     let fontRef = CTFontCreateWithName(self.font.fontName as CFStringRef, self.font.pointSize, nil) 
     let attributes = NSDictionary(dictionary: [kCTFontAttributeName : fontRef]) 
     let attributeString = NSAttributedString(string: text!, attributes: attributes as? [String : AnyObject]) 
     let frameSetterRef = CTFramesetterCreateWithAttributedString(attributeString as CFAttributedStringRef) 

     var characterFitRange:CFRange 

     CTFramesetterSuggestFrameSizeWithConstraints(frameSetterRef, CFRangeMake(0, 0), nil, CGSizeMake(bounds.size.width, CGFloat(numberOfLines)*font.lineHeight), &characterFitRange) 

     return characterFitRange.length 
    } 
}