SwiftRichString icon indicating copy to clipboard operation
SwiftRichString copied to clipboard

Interface Builder support for macOS

Open Maschina opened this issue 6 years ago • 1 comments

Is there any possibility to implement Interface Builder support for macOS or NSTextField?

Maschina avatar Jul 23 '19 14:07 Maschina

I have made a first try by adding the following to the UIKit+Extras.swift:

elseif os(macOS)

import Cocoa

internal enum IBInterfaceKeys: String {
    case styleName = "SwiftRichString.StyleName"
    case styleObj = "SwiftRichString.StyleObj"
}

extension NSTextField {
    
    /// The name of a style in the global `NamedStyles` registry.
    @IBInspectable
    public var styleName: String? {
        get { return getAssociatedValue(key: IBInterfaceKeys.styleName.rawValue, object: self) }
        set {
            set(associatedValue: newValue, key: IBInterfaceKeys.styleName.rawValue, object: self)
            self.style = StylesManager.shared[newValue]
        }
    }
    
    /// Style instance to apply. Any change of this value reload the current text of the control with set style.
    public var style: StyleProtocol? {
        set {
            set(associatedValue: newValue, key: IBInterfaceKeys.styleObj.rawValue, object: self)
            self.styledText = self.stringValue
        }
        get {
            if let innerValue: StyleProtocol? = getAssociatedValue(key: IBInterfaceKeys.styleObj.rawValue, object: self) {
                return innerValue
            }
            return StylesManager.shared[self.styleName]
        }
    }
    
    /// Use this to render automatically the text with the currently set style instance or styleName.
    public var styledText: String? {
        get {
            return attributedStringValue.string
        }
        set {
            guard let text = newValue else { return }
            let style = self.style ?? Style()
            self.attributedStringValue = style.set(to: text, range: nil)
        }
    }
    
}

#endif

It shows the property in the IB of course, but this implementation will not have any effect on the final styling in the Interface Builder.

Any ideas?

Maschina avatar Jul 23 '19 15:07 Maschina