How to Apply Uiappearance Proxy Properties to Uilabel

How do I apply UIAppearance Proxy properties to UILabel?

OK, it turns out that you cannot style any UILabel properties using the UIAppearance proxy.

While the UILabel class conforms to the UIAppearanceContainer protocol, a check of UILabel.h shows that none of its properties are marked with UI_APPEARANCE_SELECTOR, the prerequisite for the use of UIAppearance.

Bugger.

Styling UILabel using UIAppearance proxy

I think you can try in your subclass something like:

UIFont *newFont = [UIFont fontWithName:@"YourFontName" size:self.font.pointSize];
self.font = newFont

UIAppearance of UILabel whithin UICollectionViewCell - selected/unselected

Finally found a Workaround for this issue. I post my solution it may help someone.

first I define the color for both textColor and highlightedTextColor

UILabel.appearance(whenContainedInInstancesOf: [MyCollectionViewCell.self]).textColor = colorScheme.color1
UILabel.appearance(whenContainedInInstancesOf: [MyCollectionViewCell.self]).highlightedTextColor = colorScheme.color2

Then in MyCollectionViewCell I "bind" the isSelected to UILabel isHighlighted :

class MyCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var myLabel: UILabel!

override var isSelected: Bool {
didSet {
self.myLabel.isHighlighted = isSelected
}
}
}

Works flawlessly.

Hope this can help someone

What properties can I set via an UIAppearance proxy?

Properties that can be set via UIAppearance are tagged with UI_APPEARANCE_SELECTOR in the corresponding header file.

To generate a list of properties tagged with UI_APPEARANCE_SELECTOR, you can use the following unix commands in a terminal:

cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer‌​/SDKs/iPhoneOS5.1.sdk/System/Library/Frameworks/UIKit.framework/Headers
grep -H UI_APPEARANCE_SELECTOR ./* | sed 's/ __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;//'

Here is a list that I compiled using the above commands. This is for the 5.0 SDK. The corresponding getter methods have been omitted for brevity.


UIActivityIndicatorView

@property (readwrite, nonatomic, retain) UIColor *color

UIBarButtonItem

@property(nonatomic,retain) UIColor *tintColor
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
- (void)setBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics
- (void)setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
- (void)setBackButtonTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics
- (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics

[iOS 6.0]
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state style:(UIBarButtonItemStyle)style barMetrics:(UIBarMetrics)barMetrics [iOS 6.0]

UIBarItem

- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state

UINavigationBar

@property(nonatomic,retain) UIColor *tintColor
@property(nonatomic,copy) NSDictionary *titleTextAttributes
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics

[iOS 6.0]
@property(nonatomic,retain) UIImage *shadowImage

[iOS 7.0]
@property(nonatomic,retain) UIColor *barTintColor
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
- (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
@property(nonatomic,retain) UIImage *backIndicatorImage
@property(nonatomic,retain) UIImage *backIndicatorTransitionMaskImage

UIPageControl

[iOS 6.0]
@property(nonatomic,retain) UIColor *pageIndicatorTintColor
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor

UIProgressView

@property(nonatomic, retain) UIColor* progressTintColor    
@property(nonatomic, retain) UIColor* trackTintColor
@property(nonatomic, retain) UIImage* progressImage
@property(nonatomic, retain) UIImage* trackImage

UIRefreshControl

[iOS 6.0]
@property (nonatomic, retain) UIColor *tintColor
@property (nonatomic, retain) NSAttributedString *attributedTitle

UISearchBar

@property(nonatomic,retain) UIImage *backgroundImage
@property(nonatomic,retain) UIImage *scopeBarBackgroundImage
@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment
@property(nonatomic) UIOffset searchTextPositionAdjustment
- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
- (void)setScopeBarButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state
- (void)setScopeBarButtonDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState
- (void)setScopeBarButtonTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state
- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon

[iOS 7.0]
@property(nonatomic,retain) UIColor *barTintColor
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
- (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics

UISegmentedControl

@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
- (void)setDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state
- (void)setContentPositionAdjustment:(UIOffset)adjustment forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone barMetrics:(UIBarMetrics)barMetrics

UISlider

@property(nonatomic,retain) UIColor *minimumTrackTintColor
@property(nonatomic,retain) UIColor *maximumTrackTintColor
@property(nonatomic,retain) UIColor *thumbTintColor

UIStepper

[iOS 6.0]
@property(nonatomic,retain) UIColor *tintColor
- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state
- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState
- (void)setIncrementImage:(UIImage *)image forState:(UIControlState)state
- (void)setDecrementImage:(UIImage *)image forState:(UIControlState)state

UISwitch

@property(nonatomic, retain) UIColor* onTintColor

[iOS 6.0]
@property(nonatomic, retain) UIColor *tintColor
@property(nonatomic, retain) UIColor *thumbTintColor
@property(nonatomic, retain) UIImage *onImage
@property(nonatomic, retain) UIImage *offImage

UITabBar

@property(nonatomic,retain) UIColor *tintColor
@property(nonatomic,retain) UIColor *selectedImageTintColor
@property(nonatomic,retain) UIImage *backgroundImage
@property(nonatomic,retain) UIImage *selectionIndicatorImage

[iOS 6.0]
@property(nonatomic,retain) UIImage *shadowImage

[iOS 7.0]
@property(nonatomic,retain) UIColor *barTintColor
@property(nonatomic) UITabBarItemPositioning itemPositioning
@property(nonatomic) CGFloat itemWidth
@property(nonatomic) CGFloat itemSpacing
@property(nonatomic) UIBarStyle barStyle

UITabBarItem

- (void)setTitlePositionAdjustment:(UIOffset)adjustment

UITableView

[iOS 6.0]
@property(nonatomic, retain) UIColor *sectionIndexColor
@property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor

[iOS 7.0]
@property (nonatomic) UIEdgeInsets separatorInset
@property(nonatomic, retain) UIColor *sectionIndexBackgroundColor

UITableViewCell

[iOS 7.0]
@property (nonatomic) UIEdgeInsets separatorInset

UITableViewHeaderFooterView

[iOS 6.0]
@property(nonatomic, retain) UIColor *tintColor

UIToolbar

@property(nonatomic,retain) UIColor *tintColor;
- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics

[iOS 6.0]
- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIToolbarPosition)topOrBottom

[iOS 7.0]
@property(nonatomic,retain) UIColor *barTintColor

(July 3, 2012): This list hasn't changed up til the most recent version of XCode (4.3.3).


(March 12, 2013): List updated for SDK 6.1. Properties that apply for iOS 6.0 (or greater) are listed under the [iOS 6.0] tag.


(Oct. 24, 2013): List updated for SDK 7.0. Properties that apply for iOS 7.0 (or greater) are listed under the [iOS 7.0] tag.

Overlook UIAppearance proxy for random subviews & not whole class

I explicitly changed expected text alignment for UILabel in "viewWillAppear" for any controller & for table view cell,I applied same changes at "awakeFromNib" & it worked fine. I tried to do it in "layoutSubview" method which DID NOT work.

I believe if you are creating any UILabel programmatically then you can set specific textAlignment which will override UIAppearance proxy work. I have not tested this part.

I hope this will be helpful & save someones time.

How do I remove UIAppearance settings from UILabel?

In swift 4 i do as follow(just setting nil to it):

UILabel.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).textColor = nil

UIAppearance has no effect on Label text color

Referring to this, UIAppearance kind of doesn't really seem work with UILabel...

Since you're subclassing from UILabel, maybe it would make sense to set textcolor property initWithFrame: method on in your MessageLabel class?

Or another option, since you say these MessageLabel instances are used for UITableViewCell's background, maybe it would make sense to leave label's background clear and change background of cell itself, for example in tableView:willDisplayCell:forRowAtIndexPath: method in tableView's delegate?

How to make UIAppearance proxies apply to programmatically created views?

After some searching...

Apparently, the fact that UI elements (such as labels) created in Interface Builder are affected by .appearance() settings is not really intentional, and is probably not reliable.

In fact, the documentation seems rather confusing and misleading.

To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.

The only classes that adopt UIAppearance protocol are

  • UIBarItem
  • UIView

and the only classes that adopt UIAppearanceContainer protocol are

  • UIPopoverController
  • UIPresentationController
  • UIView
  • UIViewController

So... best bet is probably to sub-class your labels.

UILabel Appearance in Monotouch

It seems like this problem is not specific to MonoTouch.

Quoting Joshua from another thread,

OK, it turns out that you cannot style any UILabel properties using the UIAppearance proxy.

While the UILabel class conforms to the UIAppearanceContainer protocol, a check of UILabel.h shows that none of its properties are marked with UI_APPEARANCE_SELECTOR, the prerequisite for the use of UIAppearance.

Which is annoying.



Related Topics



Leave a reply



Submit