0

Currently all my buttons and textfields have attributedText values defined to be attributed strings.

Consider the case with a simple UILabel. Whenever I have to change the text for this UILabel (based on some user action), I have to redefine the attributes on the NSAttributedString. One way is to simply create a subroutine that generates these attributes whenever I require them but that's a concern given there could be a number of different labels (or attributed strings) that would require such convenience methods.

Another could be simply changing the text field and having observers add those attributes but that's the same amount of work and now probably more complicated.

Is there a simple way of achieving the above without redefining attributes?

p0lAris
  • 4,750
  • 8
  • 45
  • 80
  • 1
    What about category? – Jun Jan 12 '15 at 12:52
  • I am not sure what you are implying. What about it? – p0lAris Jan 12 '15 at 12:52
  • You'll have to redefine your attributes each time, there is no way around it. I think what Harry was trying to say is that having a category on UILabel will avoid code duplication and allow you to expose convenience methods to update your attributed text for all the UILabel through your project. – Champoul Jan 12 '15 at 14:07

1 Answers1

1

Exploring @Harry's ideas, here are a few ideas :

Category on NSAttributedString, category on UILabel, or category on NSDictionary, and maybe a mix of them, according to which one best suits you and your project. Using a category on NSAttributedString in priority before UILabel could be more interesting in case you wanted to use the custom NSAttributedString for others kind of object (like a UITextView).

A good start:

typedef enum : NSUInteger {
    AttributeStyle1,
    AttributeStyle2,
} AttributeStyles;

A possible category method on NSDictionary:

-(NSDictionary *)attributesForStyle:(AttributeStyles)style
{
    NSDictionary *attributes;
    switch(style)
    {
        case AttributeStyle1:
            attributes = @{}//Set it
            break;
        case AttributeStyle2:
            attributes = @{}//Set it
            break;
        default:
            attributes = @{}//Set it
            break;
    }
    return attributes;
}

Category possible on UILabel:

-(void)setString:(NSString *)string withAttributes:(NSDictionary *)attributes
{
    [self setAttributedText:[[NSAttributedString alloc] initWithString:string attributes:attributes];
}

Category possible on NSAttributedString:

-(NSAttributedString  *)initWithString:(NSString *)string withStyle:(AttributedStyles)style
{
    //Here, a mix is possible using the first method, or doing here the switch case
    //Ex:  return [[NSAttributedString alloc] initWithString:string attributes:[NSDictionary attributesForStyle:style];
    //And to use like this: [yourLabel setAttributedText:[[NSAttributedString alloc] initWithString:string withStyle:AttributeStyle1];
}
Larme
  • 24,190
  • 6
  • 51
  • 81
  • Thanks. I actually explored all of these options. The only issue is that there are different types of labels/textfields; i.e. they require different fonts, etc; I guess I could include this in the setter and initializer as well. Fair enough! – p0lAris Jan 12 '15 at 18:50