2012-09-20 35 views
5

Estoy seguro de que mutable significa que se puede cambiar, entonces, ¿por qué sucede esto?NSMutableAttributedString se bloquea al cambiar la fuente?

attrString = [[NSMutableAttributedString alloc] initWithString:@"Tip 1: Aisle Management The most obvious step – although one that still has not been taken by a disconcerting number of organisations – is to configure cabinets in hot and cold aisles. If you haven’t got your racks into cold and hot aisle configurations, we can advise ways in which you can achieve improved airflow performance."]; 

     [attrString setFont:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 23)]; 
     [attrString setFont:[UIFont systemFontOfSize:15] range:NSMakeRange(24, 325)]; 
     [attrString setTextColor:[UIColor blackColor] range:NSMakeRange(0,184)]; 
     [attrString setTextColor:[UIColor blueColor] range:NSMakeRange(185,325)]; 
     break; 

Tanto mi y mi catextlayer nsmutableattributedsring se definen en mi archivo de cabecera. Hago los cambios en mi cadena anterior en un interruptor, y luego llamar a este código para actualizar el catextlayer la cadena se muestra en:

//updates catext layer 
TextLayer = [CATextLayer layer]; 

TextLayer.bounds = CGRectMake(0.0f, 0.0f, 245.0f, 290.0f); 
TextLayer.string = attrString; 
TextLayer.position = CGPointMake(162.0, 250.0f); 
TextLayer.wrapped = YES; 

[self.view.layer addSublayer:TextLayer]; 

Se estrella en cuando se trata de establecer la fuente, pero no puedo entender por qué ?

- [NSConcreteMutableAttributedString setFont: Gama:]: Selector no reconocido enviado a la instancia 0xd384420 * Terminación de aplicación debido a excepción no detectada 'NSInvalidArgumentException', razón: '- [NSConcreteMutableAttributedString setFont: Gama:]: Selector no reconocido envió por ejemplo 0xd384420 '

¿Por qué sucede esto?

+0

Aquí hay un código gratuito que muestra el uso de cadenas atribuidos: github.com/artmayes167/Attribute – AMayes

Respuesta

10

NSMutableAttributedString no tiene un setFont: range: function.

Tomado de aquí .... iphone/ipad: How exactly use NSAttributedString?

así que hice un poco de lectura de los documentos.

Las funciones es ...

[NSMutableAttirbutedString setAttributes:NSDictionary range:NSRange]; 

lo que debería ser capaz de hacer algo como esto ...

[string setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetice-Neue"]} range:NSMakeRange(0, 2)]; 

o

[string setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetice-Neue"], NSFontAttributeName", nil] range:NSMakeRange(0, 2)]; 

si todavía usando la vieja sintaxis ObjC.

Espero que ayude.

+0

me sale un error en el uso de NSFontAttributeName no declarado? No he probado los colores todavía. – dev6546

+0

editado después de leer documentos. https://developer.apple.com/library/mac//#/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html – Fogmeister

+0

Creo que esos documentos solo funcionan para os x 10+ que también causa un choque. – dev6546

1

En primer lugar, ¿dijo attrString que es una propiedad? Si es una propiedad, será mejor que compruebe que ha declarado la propiedad con el atributo copy, y ¿presumiblemente está utilizando el setter generado por el compilador? En caso afirmativo, el colocador generado por el compilador envía el mensaje de copia al objeto para hacer una copia. El mensaje de copia hace una copia inmutable. Es decir, crea un NSAttributedString, no un NSMutableAttributedString.

Una forma de solucionar este problema es escribir su propia incubadora que utiliza mutableCopy, como este si está utilizando ARC:

- (void)setTextCopy:(NSMutableAttributedString *)text { 
    textCopy = [text mutableCopy]; 
} 

o como esto si usted está utilizando el conteo manual de referencia:

- (void)setTextCopy:(NSMutableAttributedString *)text { 
    [textCopy release]; 
    textCopy = [text mutableCopy]; 
} 

Otra solución sería hacer que textCopy sea un NSAttributedString en lugar de un NSMutableAttributedString y hacer que el resto del código trabaje con él como un objeto inmutable.

Referencia: 1️⃣ How to copy a NSMutableAttributedString 2️⃣ NSConcreteAttributedString mutableString crash

Cuestiones relacionadas