Ios 如何将字符数限制为textfild width。粘贴文本时,如何剪切文本字段宽度以外的文本?

Ios 如何将字符数限制为textfild width。粘贴文本时,如何剪切文本字段宽度以外的文本?,ios,swift,uitextfield,swift4,textfield,Ios,Swift,Uitextfield,Swift4,Textfield,将长度限制为textfield中的字符数会导致UIlabel长度因不同语言而异 因此,我想让它可以只作为一个固定的文本字段宽度键入 如何将字符数限制为文本字段宽度?粘贴文本时,如何剪切文本字段宽度以外的文本 这是我的密码 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if subtitl

将长度限制为textfield中的字符数会导致UIlabel长度因不同语言而异


因此,我想让它可以只作为一个固定的文本字段宽度键入

如何将字符数限制为文本字段宽度?粘贴文本时,如何剪切文本字段宽度以外的文本

这是我的密码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if subtitleTextfield.text != nil {
   nsString = subtitleTextfield.text! as NSString
   nsString = nsString.replacingCharacters(in: range, with: string) as NSString
    }   else if subtitleTextfield.text != ""  {
   nsString = subtitleTextfield.text! as NSString
   nsString = nsString.replacingCharacters(in: range, with: string) as NSString

    } else if subtitleTextfield.text == "" {
        subtitleTextfield.text = ""
    }

    guard let texts = subtitleTextfield.text else { return true }
    let currentText = nsString as NSString
    if currentText.length > 45 {
        subtitleTextfield.text = currentText.substring(to: 45)
    }

    return currentText.length < 45
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
如果subtitlettextfield.text!=nil{
nsString=subtitleTextfield.text!作为nsString
nsString=nsString.replacingCharacters(在:范围内,带:字符串)作为nsString
}如果字幕为textfield.text,则为else!“”{
nsString=subtitleTextfield.text!作为nsString
nsString=nsString.replacingCharacters(在:范围内,带:字符串)作为nsString
}如果subtitlettextfield.text==“”,则为else{
subtitlettextfield.text=“”
}
guard let text=subtitleTextfield.text else{return true}
将currentText=nsString设为nsString
如果currentText.length>45{
subtitleTextfield.text=currentText.substring(至:45)
}
返回currentText.length<45
}

此问题的可能重复我想使键入和粘贴文本仅作为固定的文本字段宽度(而不是字符数)成为可能。文本大小不是固有的吗,也就是说,如果需要的话,它会变小,以便在字段中容纳更多的文本?可能与此问题重复我想让它能够仅以固定的文本字段宽度(而不是字符数)键入和粘贴文本。文本大小不是固有的,即如果需要,它会变小,以便在字段中容纳更多的文本吗?