Swift-UITextView占位文字及占位文字颜色扩展(可在xib中直接使用)

Swift-UITextView占位文字及占位文字颜色扩展(可在xib中直接使用),第1张

概述使用案例 ``` extension UITextView {     ```     private struct RuntimeKey {         static let hw_placeholderLabelKey = UnsafeRawPointer.init(bitPattern: "hw_placeholderLabelKey".hashValue)         /// ..

使用案例

```

extension UITextVIEw {

    ```

    private struct RuntimeKey {

        static let hw_placeholderLabelKey = UnsafeRawPointer.init(bitPattern: "hw_placeholderLabelKey".hashValue)

        /// ...其他Key声明

    }

    /// 占位文字

    @IBInspectable public var placeholder: String {

        get {

            return self.placeholderLabel.text ?? ""

        }

        set {

            self.placeholderLabel.text = newValue

        }

    }

    

    /// 占位文字颜色

    @IBInspectable public var placeholdercolor: UIcolor {

        get {

            return self.placeholderLabel.textcolor

        }

        set {

            self.placeholderLabel.textcolor = newValue

        }

    }

    

    private var placeholderLabel: UILabel {

        get {

            var label = objc_getAssociatedobject(self,UITextVIEw.RuntimeKey.hw_placeholderLabelKey!) as? UILabel

            if label == nil {

                if (self.Font == nil) {

                    self.Font = UIFont.systemFont(ofSize: 14)

                }

                label = UILabel.init(frame: self.bounds)

                label?.numberOflines = 0

                label?.Font = self.Font

                label?.textcolor = UIcolor.lightGray

                self.addSubvIEw(label!)

                self.setValue(label!,forKey: "_placeholderLabel")

                objc_setAssociatedobject(self,UITextVIEw.RuntimeKey.hw_placeholderLabelKey!,label!,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

                self.sendSubvIEw(toBack: label!)

            }

            return label!

        }

        set {

            objc_setAssociatedobject(self,newValue,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        }

    }

}

``` 

总结

以上是内存溢出为你收集整理的Swift-UITextView占位文字及占位文字颜色扩展(可在xib中直接使用)全部内容,希望文章能够帮你解决Swift-UITextView占位文字及占位文字颜色扩展(可在xib中直接使用)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1020685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存