0

I have this code and it's working (answer from https://stackoverflow.com/a/3586943/1187014), but I want try modify it a little bit :

NSString *text = @"Forgot password?";

if ([_labelForgotPassword respondsToSelector:@selector(setAttributedText:)])
{
    // iOS6 and above : Use NSAttributedStrings
    const CGFloat fontSize = 13;
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
    UIColor *foregroundColor = [UIColor whiteColor];

    // Create the attributes
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                           boldFont, NSFontAttributeName,
                           foregroundColor, NSForegroundColorAttributeName, nil];
    NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                              regularFont, NSFontAttributeName, nil];
    const NSRange range = NSMakeRange(16,0);

    // Create the attributed string (text + attributes)
    NSMutableAttributedString *attributedText =
    [[NSMutableAttributedString alloc] initWithString:text
                                           attributes:attrs];
    [attributedText setAttributes:subAttrs range:range];

    // Set it in our UILabel and we are done!
    [_labelForgotPassword setAttributedText:attributedText];
} else {
    // iOS5 and below
    // Here we have some options too. The first one is to do something
    // less fancy and show it just as plain text without attributes.
    // The second is to use CoreText and get similar results with a bit
    // more of code. Interested people please look down the old answer.

    // Now I am just being lazy so :p
    [_labelForgotPassword setText:text];

}

What if I have multiple text, let's say :

NSString *text1 = @"Forgot password?"; // relates with UILabel _labelForgotPassword
NSString *text2 = @"I agree with terms and condition"; // relates with UILabel _labelTerms
NSString *text3 = @"Your country is not listed yet?"; // relates with UILabel _labelCountry

First method that came into my mind is by having nested IF, but nested IF will be very ugly when I have lots of text which need to be attributed, right?

So, how to create that code into a method so that I can just supply the string, name of _label, range, etc and return the result to specific UILabel. and it's all triggered under viewDidLoad. Not by button pressed, or something else.

thank you.

Community
  • 1
  • 1
Saint Robson
  • 5,475
  • 18
  • 71
  • 118

1 Answers1

0

What i am understanding is you want the same attributed logic to be applied to all labels. You can create a category on UILabel if you want to use it for only UILabels.

Syntax of the category should be something like this:

+(NSMutableAttributedString *) convertToAttributedText: (NSString *) text withFont: (UIFont *) font
{
   // write the above logic here
   //return the attributed text;
}

You can pass the text1 / text2 / text3 whatever to this api and you will get the attributed text.

label.attributedtext = [NSString convertToAttributedText: text withFont:font];

You can configure this API parameters based on your need.

Hope this helps.

Srivathsa
  • 606
  • 10
  • 34