Attributed String sans NSAttributedString

Nebuchad34Nebuchad34 Membre
juillet 2009 modifié dans Apple Developer Programs #1
Bonjour !

J'ai constaté, hier, qu'Apple n'avait pas fait don à  son iPhone SDK de la classe NSAttributedString !

Vraiment dommage, car on perd alors la possibilité de mettre facilement du texte enrichi dans le titre des UITableViewCell par exemple. J'avais en fait besoin de mettre tout bêtement des lettres majuscules en indice du titre de ma cell !

J'ai donc réfléchi un peu et trouvé une solution, mais je ne sais pas si elle est tip top niveau performance.

J'ai ainsi créé une sous classe de UIWebView appellée AttributedStringCellView

Avec une méthode de classe, je lui demande de me retourner une vue adaptée aux cells, selon que la TableView et en Style Grouped ou Plain.
J'ajoute cette vue en subView de la cell dans la définition de la reusableCell

A chaque fois que le CellForRow est appellé, je n'ai plus qu'à  faire appel à  une méthode "setURLString:" sur mon objet AttributedStringCellView qui va, derrière, appeler la méthode loadURL:baseURL: de UIWebView, mais en vérifiant qu'on ne charge pas le même texte (car une UIWebView c'est assez lourd...)

Avec l'astuce ci-dessus, vu que c'est pour charger des titres de cellules, mes UIWebView ne sont en fait réellement "loadées" qu'une fois.

Qu'en pensez vous ?? Si quelqu'un a une idée pour faire quelque chose de plus léger, je suis prenneur.


Voilà  le code de la dite Classe :
<br />//&nbsp; AttributedStringCellView.h<br /><br />#import &lt;UIKit/UIKit.h&gt;<br /><br />#define AttributedStringViewTag 1<br /><br />@interface AttributedStringCellView : UIWebView {<br />	NSString *currentHTMLString;<br />}<br /><br />@property (retain) NSString *currentHTMLString;<br /><br />+(AttributedStringCellView*)attributedStringWithHTMLString:(NSString*)string forTableViewStyle:(UITableViewStyle)style;<br /><br />+(NSString*)formattedStringWithHTMLString:(NSString*)string;<br /><br />-(void)setHTMLString:(NSString*)string;<br /><br />@end<br /><br />//&nbsp; AttributedStringCellView.m<br /><br />#import &quot;AttributedStringCellView.h&quot;<br /><br /><br />@implementation AttributedStringCellView<br /><br />@synthesize currentHTMLString;<br /><br />+(AttributedStringCellView*)attributedStringWithHTMLString:(NSString*)string forTableViewStyle:(UITableViewStyle)style;<br />{<br />	NSInteger x = 5;<br />	<br />	if (style == UITableViewStyleGrouped)<br />		x = 15;<br />		<br />	AttributedStringCellView *webView = [[AttributedStringCellView alloc] initWithFrame:CGRectMake(x, 2, 150, 40)];<br />	webView.opaque = NO;<br />	webView.backgroundColor = [UIColor clearColor];<br />	webView.tag = AttributedStringViewTag;<br />	[webView loadHTMLString:[AttributedStringCellView formattedStringWithHTMLString:string] baseURL:nil];<br />	webView.currentHTMLString = string;<br />	<br />	<br />	return [webView autorelease];<br />}<br /><br />+ (NSString*)formattedStringWithHTMLString:(NSString*)string {<br />	return [NSString stringWithFormat:@&quot;&lt;style type=&#092;&quot;text/css&#092;&quot;&gt;html {font-size: 18px;font-family: Helvetica, Arial, Geneva;font-weight: bold;}&lt;/style&gt; %@&quot;, string];<br />}<br /><br />- (void)setHTMLString:(NSString*)string {<br />	if ([currentHTMLString isEqualToString:string])<br />		return;<br />	else<br />		[self loadHTMLString:[AttributedStringCellView formattedStringWithHTMLString:string] baseURL:nil];<br />}<br /><br />- (void)dealloc {<br />&nbsp; &nbsp; [currentHTMLString release];<br />&nbsp; &nbsp; [super dealloc];<br />}<br /><br />


Et voilà  l'utilisation dans mon CellForRow :
<br />- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath<br />{<br />	static NSString *CellIdentifier = @&quot;Cell&quot;;<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];<br />&nbsp; &nbsp; if (cell == nil) {<br />		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];<br />		[cell addSubview:[AttributedStringCellView attributedStringWithHTMLString:nil forTableViewStyle:[tView style]]];<br />&nbsp; &nbsp; }<br />	<br />	NSString *URLString;<br />	<br />	//Ici je choisi de manière conditionnelle la valeur de URLString, par exemple <br />&nbsp; &nbsp; &nbsp; &nbsp; // Admettons URLString = @&quot;X&lt;sub&gt;S&lt;/sub&gt;&quot;; (coordonnée X d&#39;un point S....)<br />	<br />	AttributedStringCellView *attributedStringView = (AttributedStringCellView*)[cell viewWithTag:AttributedStringViewTag];<br />	[attributedStringView setHTMLString:URLString];<br /><br />	return cell;<br />}<br />

Réponses

  • Philippe49Philippe49 Membre
    08:00 modifié #2
    Et tu as essayé avec les méthodes de la catégorie NSString (UIKit additions) comme
    -(void)drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:

  • AliGatorAliGator Membre, Modérateur
    08:00 modifié #3
    oui, ou en personnalisant ta UITableViewCell pour mettre plusieurs UILabels dedans, comme conseillé dans le UITableView programming guide, un pour ton titre en majuscule et l'autre pour le reste ?
  • Nebuchad34Nebuchad34 Membre
    08:00 modifié #4
    Mettre plusieurs UILabel me parait peu pratique, puisque les indices ne sont pas toujours placés au même endroit.

    Pour ce qui est des additions de NSString, je ne connaissais pas. C'est à  voir effectivement. C'est en tout cas une piste interessante. Pour l'usage actuel, le UIWebView fonctionne bien. Mais comme cela risque de faire lourd si je dois le réutiliser de manière plus intensive, à  ce moment là  je jetterai un oeil du côté de
    -(void)drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:

    Merci pour l'info.
Connectez-vous ou Inscrivez-vous pour répondre.