HOW TO DO FORMATTING WITH UIACTIONSHEET TITLE STRING

zack mathews

Total Post:23

Points:161
Posted by  zack mathews
 800  View(s)
Ratings:
Rate this:

Can we do formatting in the UIActionSheet title string.

For example, if we have title string in two words like 'Action Sheet',

now I want word 'Action' should be Bold (with a particular font size) and the second string(Sheet) should be Regular (no change).


Can we do like that?

If yes then please tell me.!

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to do formatting with UIActionSheet title string

    Yes, you can do formatting with the action sheet title,

    look my example I have created action sheet and give the title 'Action Sheet Demo'
    and use like my code, here is my code:

    - (NSAttributedString *) actionSheetAttributedTitle;

    {

        NSMutableAttributedString *attString1=[[NSMutableAttributedString alloc]

    initWithString:@"Action "];

        NSMutableAttributedString *attString2=[[NSMutableAttributedString alloc]

    initWithString:@"Sheet "];

        NSMutableAttributedString *attString3=[[NSMutableAttributedString alloc]

    initWithString:@"Demo"];

     

        UIFont *font1=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];

        UIFont *font2=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];

        UIFont *font3=[UIFont fontWithName:@"Helvetica-Bold" size:12.0f];

     

        [attString1 addAttribute:NSFontAttributeName value:font1

    range:NSMakeRange(0,

    attString1.length)];

        [attString1 addAttribute:NSForegroundColorAttributeName

    value:[UIColor redColor]

    range:NSMakeRange(0,

    attString1.length)];

     

        [attString2 addAttribute:NSFontAttributeName value:font2

    range:NSMakeRange(0,

    attString2.length)];

        [attString2 addAttribute:NSForegroundColorAttributeName

    value:[UIColor blueColor]

                                             range:NSMakeRange(0,

    attString2.length)];

        

        [attString3 addAttribute:NSFontAttributeName value:font3

    range:NSMakeRange(0,

    attString3.length)];

        [attString3 addAttribute:NSForegroundColorAttributeName

    value:[UIColor yellowColor]

    range:NSMakeRange(0,

    attString3.length)];

        

        [attString1 appendAttributedString:attString2];

        [attString1 appendAttributedString:attString3];

     

        return [attString1 copy];

    }

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet;

    {

            UILabel *sheetTitleLabel;

            if([actionSheet respondsToSelector:@selector(_titleLabel)]) {

            sheetTitleLabel = objc_msgSend(actionSheet, @selector(_titleLabel));

            sheetTitleLabel.attributedText = [self actionSheetAttributedTitle];        

        }

    }

      Modified On Nov-28-2015 12:12:05 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!