2017-07-04 5 views
1

私は、ユーザが、異なるサイズのテキストの物乞いで追加する必要があり、小さな「R $を」入力完了したときに、私がしたいカスタムのUITextFieldを持っているにNSMutableAttributedStringを追加します。のTextField

私はこのような「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 $" bユタ40

の大きさは、それは私がNSMutableAttributedStringを使用しようとしているものを行うことは可能ですか?

答えて

1

私はあなたの質問に取り組んでいましたが、私はUITextFieldにバグがあると思います。より大きなフォントにフォントを変更すると効果がありますが、小さいフォントでは動作しません。

私は、カスタムクラスをやって、コメントを追加し、いくつかのカスタマイズ可能な検査可能な特性を、これが最終的にこれがどのように見えるかです

あなたに助けを願って、注意をしている:グリッチがあるため、私のGIFコンバータである enter image description here

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 
    } 

} 

ホープこれは

+0

は申し訳ありません...まだそれは奇妙だ –

+0

動作しない場合に役立ちます私たちので、電子resultAttributedString.addAttribute(NSUnderlineStyleAttributeName、値:NSUnderlineStyle.styleSingle.rawValue、範囲:範囲) は、それが動作します...しかし、フォントを変更すると、代わりに私がツァッフィーノフォントを試してみましたシステムフォントを使用しての –

+0

を動作しません...その文字列全体Zapfinoに変わった... ... 'しかし、その後、それは'のsetTextない何らかの理由:: 'をした後、使用 –