2017-08-17 4 views
1

Je travaille sur une fonctionnalité, que je peux définir mon style personnalisé sur UILabel (en enregistrant le nom du style). J'ai un code simple pour ceci:iOS @IBDesignable autorefresh UILabel paramètres comme titre, police, couleur

struct Style { 
    var fontName: String! 
    var fontSize: CGFloat! 
    var color: UIColor! 
    var styleName: String! 
    var font: UIFont { 
     return UIFont(name: self.fontName, size: self.fontSize)! 
    } 

    init(styleName: String, fontName: String!, fontSize: CGFloat!, color: UIColor!) { 
     self.fontName = fontName 
     self.fontSize = fontSize 
     self.color = color 
    } 

    var desc: String { 
     return "Style: \(styleName)" 
    } 
} 



let dictStyle: [String: Style] = [ 
       "normal": Style(styleName: "normal", fontName: "HelveticaNeue-Light", fontSize: 20, color: UIColor.red), 
       "bold": Style(styleName: "bold", fontName: "HelveticaNeue-Bold", fontSize: 30, color: UIColor.black) 
      ] 

@IBDesignable class Label: UILabel { 

    @IBInspectable var style: String = "normal" { 
     didSet { 
      setNeedsDisplay() 
     } 
    } 

    override func prepareForInterfaceBuilder() { 
     super.prepareForInterfaceBuilder() 

     self.text = "\(style)" // there is normal or bold 

     if let color = dictStyle[self.style]?.color { 
      self.textColor = color 
     } 
     if let font = dictStyle[self.style]?.font { 
      self.font = font 
     } 
     if let name = dictStyle[self.style]?.styleName { 
      self.text = name 
     } 
    } 
} 

et il est un travail correct, mais je voudrais titre de changement automatique, la police, la couleur dans Interface Builder (en rect rouge ci-dessous).

Interface builder

J'ai vu dans le fichier .storyboard

<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9lO-cw-I2Z" customClass="Label" customModule="AutoRefresh" customModuleProvider="target"> 
          <rect key="frame" x="24" y="44" width="327" height="24"/> 
          <fontDescription key="fontDescription" name="HelveticaNeue" family="Helvetica Neue" pointSize="32"/> 
          <color key="textColor" red="0.25098040700000002" green="0.50196081400000003" blue="0.0" alpha="0.76206656679999996" colorSpace="calibratedRGB"/> 
          <nil key="highlightedColor"/> 
          <userDefinedRuntimeAttributes> 
           <userDefinedRuntimeAttribute type="string" keyPath="style" value="normal"/> 
          </userDefinedRuntimeAttributes> 
         </label> 

donc il que les champs "texte" ou "textColor" ne changent pas.

Répondre

0

Voici ce que vous cherchez à Swift 4:

@IBDesignable 
class Label: UILabel { 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     config() 
    } 

    override func prepareForInterfaceBuilder() { 
     super.prepareForInterfaceBuilder() 
     config() 
    } 

    func config() { 
     // Setup how you please, e.g. color = UIColor.black 
    } 

}