ios - Set text attributes via UIAppearance in a custom UIView subclass in Swift -


accoding nshipster, "having custom ui classes conform uiappearance not best-practice, demonstrates level of care being put implementation."

therefore, i'm trying set text attributes, later used create nsattributedstring, property var titletextattributes: [string : anyobject]? in uiview subclass this:

func applyappearance() {      uinavigationbar.appearance().translucent = true     uinavigationbar.appearance().tintcolor = uicolor(named: .navbartextcolor)     uinavigationbar.appearance().bartintcolor = uicolor(named: .navbarblue)     uinavigationbar.appearance().titletextattributes = [         nsforegroundcolorattributename: uicolor(named: .navbartextcolor),         nsfontattributename: uifont.navbartitlefont()!     ]      actionbarview.appearance().backgroundcolor = uicolor(white: 1, alpha: 0.15)     actionbarview.appearance().titletextattributes = [         nskernattributename: 1.29,         nsfontattributename: uifont.buttonfont()!,         nsforegroundcolorattributename: uicolor.whitecolor(),     ] } 

this snipped appdelegate.

now, when trying set actionbarview.appearance().titletextattributes = [ ... ], i'm getting following runtime error:

error when trying set text attirbutes on custom uiview subclass

what's worth mentioning setting attributes on uinavigationbar works without problems.

going uinavigationbar's header file reveals this:

/* may specify font, text color, , shadow properties title in text attributes dictionary, using keys found in nsattributedstring.h.  */ @available(ios 5.0, *) public var titletextattributes: [string : anyobject]? 

which same definition of property in actionbarview class:

class actionbarview: uiview {      var titletextattributes: [string : anyobject]?      // ... } 

so question is: there can implement property dictionary of text attributes in own uiview subclass can set via uiappearance proxy? since using ui_appearance_selector not possible in swift? , why working out-of-the-box uikit classes uinavigationbar? there kind of black magic involved?

for appearance property add dynamic keyword:

class actionbarview: uiview {      dynamic var titletextattributes: [string : anyobject] = [:]      // use actionbarview.appearance().titletextattributes  } 

and appearance property accessor methods must of form:

func propertyforaxis1(axis1: integertype, axis2: integertype, axisn: integertype) -> propertytype func setproperty(property: propertytype, foraxis1 axis1: integertype, axis2: integertype) 

Comments