Home > DeveloperSection > Forums > How to do formatting with UIActionSheet title string
zack mathews
zack mathews

Total Post:23

Points:161
Posted on    November-26-2015 6:16 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 534  View(s)
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.!



Tarun Kumar

Total Post:213

Points:1635
Supported
Posted on    November-26-2015 11:01 PM

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

Don't want to miss updates? Please click the below button!

Follow MindStick