Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextAlignmentLeft for UIAlertController

Has anyone been able to find a way to set the text alignment of the UIAlertController. More specifically, the action sheet style?

I am using this method to add pictures (icons) to the actions :

UIImage *qsimage = [UIImage imageNamed:@"snapshot.png"];
[snapshot setValue:qsimage forKey:@"image"];
//where 'snapshot' is a UIAlertAction

This works just fine, no worries, but when adding multiple actions to the controller all the text is centered but the images are left aligned creating an awkward UI experience.

I'm trying to resemble what Apple has done in the soon to be iOS 8.4 for their Music app:

pic1

Thoughts? Am I missing something simple?

edit

After doing some debugging I've realized the Alert Actions are in a UICollectionView. I can successfully change the colors of all the buttons, but can't alter the labels of the actions in terms of alignment. Here is how I accessed them:

 [[UICollectionView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor blueColor]];

I also tried using setTitleTextAttributes on the actions with NSDictionary objects and keys with no success. Any ideas?

like image 990
soulshined Avatar asked Oct 19 '22 14:10

soulshined


1 Answers

This is tested and working - UILabel+Appearance.h:

@interface UILabel (FontAppearance)
@property (nonatomic, assign) NSTextAlignment appearanceAlignment UI_APPEARANCE_SELECTOR;
@end


@implementation UILabel (FontAppearance)

-(void)setAppearanceAlignment:(NSTextAlignment)alignment {
    [self setTextAlignment:alignment];
}

-(NSTextAlignment)appearanceAlignment {
    return self.textAlignment;
}

@end

Usage:

UILabel * appearanceLabel = [UILabel appearanceWhenContainedIn:UIAlertController.class, nil];
[appearanceLabel setAppearanceAlignment:NSTextAlignmentLeft];
like image 104
Ivan Avatar answered Oct 22 '22 03:10

Ivan