将 NSMutableAttributedString 添加到 TextField

Adding NSMutableAttributedString into a TextField

我有一个我想要的自定义 UITextfield,当用户完成输入时,必须在不同大小的文本开头添加一个小 "R$"。

我调用添加 "R$" 的方法是这样的:

self.addTarget(self, action: #selector(setCurrencyLabelPosition), for: .editingDidEnd)

然后我尝试像这样更改属性和内容:

 func setCurrencyLabelPosition(){

        let fullText:String = "R$\((self.text)!)"
        self.text = fullText
        var attribute:NSMutableAttributedString = NSMutableAttributedString(attributedString:self.attributedText!)
        attribute.addAttribute(NSFontAttributeName, value:UIFont.systemFont(ofSize: 12), range: NSRange(location:  0, length: 2))
        self.attributedText = attribute

    }

此文本字段的原始文本大小设置为 40.0,我只希望 "R$" 大小为 12.0

我面临的问题是整个文本的大小为 40.0 它打印 "R$" 但大小为 40.

是否可以使用 NSMutableAttributedString 做我想做的事情?

我一直在研究你的问题,我认为 UITextField 中有一个错误,因为如果你将字体修改为更大的字体它可以工作但是如果你这样做但是对于小字体则不起作用.

我已经完成了自定义 class 并添加了一些可自定义的 Inspectable 属性,希望这最终对您有所帮助

看起来是这样,注意:问题是因为我的 gif 转换器

import UIKit

@IBDesignable
class CustomTextField: UITextField {

    @IBInspectable var prefix : String = ""
    @IBInspectable var removePrefixOnEditing : Bool = true

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.addTarget(self, action: #selector(setCurrencyLabelPosition), for: .editingDidEnd)
        self.addTarget(self, action: #selector(removePrefix), for: .editingDidBegin)
    }

    func removePrefix(){
        if self.attributedText != nil
        {
            if(self.removePrefixOnEditing)
            {
                self.defaultTextAttributes = [NSFontAttributeName : UIFont.systemFont(ofSize: 20)]
                let prefixRange = NSString(string: (self.attributedText?.string)!).range(of: prefix)
                if(prefixRange.location != NSNotFound)
                {
                    self.attributedText = NSAttributedString(string: (self.attributedText?.string.replacingOccurrences(of: prefix, with: ""))!, attributes: self.defaultTextAttributes)
                }
            }
        }
    }

    func setCurrencyLabelPosition(){

        if self.attributedText != nil
        {
            var fullText:String = "\((self.attributedText?.string)!)"
            if(NSString(string: (self.attributedText?.string)!).range(of: prefix).location == NSNotFound)
            {
                fullText = "\(prefix)\((self.attributedText?.string)!)"
            }
            //hacky part, seems to be a bug in UITextField
            self.defaultTextAttributes = [NSFontAttributeName : UIFont.systemFont(ofSize: 10)]
            self.attributedText = NSAttributedString(attributedString: self.changeFontForText(originalText: fullText, text: prefix, basicFont: UIFont.systemFont(ofSize: 20), newFont: UIFont.systemFont(ofSize: 12)))
        }
    }

    func changeFontForText(originalText:String,text:String,basicFont:UIFont, newFont:UIFont) -> NSMutableAttributedString
    {
        let resultAttributedString = NSMutableAttributedString(string: originalText, attributes: [NSFontAttributeName : basicFont])
        let range = NSString(string: originalText).range(of: text)
        if(range.location != NSNotFound)
        {
            resultAttributedString.setAttributes([NSFontAttributeName:newFont], range: range)
        }

        return resultAttributedString
    }

}

希望对您有所帮助