设置textfeild的属性

//初始化textfield并设置位置及大小
  UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
 
//设置边框样式,只有设置了才会显示边框样式  
  text.borderStyle = UITextBorderStyleRoundedRect;

  typedef enum {
    UITextBorderStyleNone, 
    UITextBorderStyleLine,
    UITextBorderStyleBezel,
    UITextBorderStyleRoundedRect  
  } UITextBorderStyle;
  
//设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉  
   text.backgroundColor = [UIColor whiteColor];
 
//设置背景
  text.background = [UIImage imageNamed:@"dd.png"];
 
//设置背景 
  text.disabledBackground = [UIImage imageNamed:@"cc.png"];

//当输入框没有内容时,水印提示 提示内容为password
  text.placeholder = @"password";
 
//设置输入框内容的字体样式和大小
  text.font = [UIFont fontWithName:@"Arial" size:20.0f];
 
//设置字体颜色
  text.textColor = [UIColor redColor];
 
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
  text.clearButtonMode = UITextFieldViewModeAlways;
 
typedef enum {
    UITextFieldViewModeNever,  重不出现
    UITextFieldViewModeWhileEditing, 编辑时出现
    UITextFieldViewModeUnlessEditing,  除了编辑外都出现
    UITextFieldViewModeAlways   一直出现
} UITextFieldViewMode;
 
//输入框中一开始就有的文字
  text.text = @"一开始就在输入框的文字";
 
//每输入一个字符就变成点 用语密码输入
  text.secureTextEntry = YES;
 
//是否纠错
  text.autocorrectionType = UITextAutocorrectionTypeNo;
 
typedef enum {
    UITextAutocorrectionTypeDefault, 默认
    UITextAutocorrectionTypeNo,   不自动纠错
    UITextAutocorrectionTypeYes,  自动纠错
} UITextAutocorrectionType;
 
//再次编辑就清空
  text.clearsOnBeginEditing = YES; 
 
//内容对齐方式
  text.textAlignment = UITextAlignmentLeft;
 
//内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
  text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
 
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动  
  textFied.adjustsFontSizeToFitWidth = YES;
 
//设置自动缩小显示的最小字体大小
  text.minimumFontSize = 20;
 
//设置键盘的样式
  text.keyboardType = UIKeyboardTypeNumberPad;
 
typedef enum {
    UIKeyboardTypeDefault,       默认键盘,支持所有字符         
    UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘
    UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
    UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,              数字键盘
UIKeyboardTypePhonePad,     电话键盘
    UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘     
UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点
    UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 
} UIKeyboardType;
 
//首字母是否大写
  text.autocapitalizationType = UITextAutocapitalizationTypeNone;
 
typedef enum {
    UITextAutocapitalizationTypeNone, 不自动大写
    UITextAutocapitalizationTypeWords,  单词首字母大写
    UITextAutocapitalizationTypeSentences,  句子的首字母大写
    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
 
//return键变成什么键
  text.returnKeyType =UIReturnKeyDone;
 
typedef enum {
    UIReturnKeyDefault, 默认 灰色按钮,标有Return
    UIReturnKeyGo,      标有Go的蓝色按钮
    UIReturnKeyGoogle,
标有Google的蓝色按钮,用语搜索
    UIReturnKeyJoin,
标有Join的蓝色按钮
    UIReturnKeyNext,
标有Next的蓝色按钮
    UIReturnKeyRoute,
标有Route的蓝色按钮
    UIReturnKeySearch,
标有Search的蓝色按钮
    UIReturnKeySend,
标有Send的蓝色按钮
    UIReturnKeyYahoo,
标有Yahoo的蓝色按钮
    UIReturnKeyYahoo,
标有Yahoo的蓝色按钮
    UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
 
//键盘外观
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
typedef enum {
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert,     深灰 石墨色
 
} UIReturnKeyType;

//设置代理 用于实现协议
  text.delegate = self;
 
//把textfield加到视图中
  [self.window addSubview:text];
 
//最右侧加图片是以下代码   左侧类似
    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    text.rightView=image;
    text.rightViewMode = UITextFieldViewModeAlways; 
 
typedef enum {
    UITextFieldViewModeNever,
    UITextFieldViewModeWhileEditing,
    UITextFieldViewModeUnlessEditing,
    UITextFieldViewModeAlways
} UITextFieldViewMode;

//按return键键盘往下收  becomeFirstResponder
 
类要采用UITextFieldDelegate协议
 
text.delegate = self;  声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在UITextFieldDelegate里所以我们要采用UITextFieldDelegate这个协议
 
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [text resignFirstResponder];    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    return YES;
}

重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
 
– textRectForBounds:      //重写来重置文字区域
– drawTextInRect:         
//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds:  
//重写来重置占位符区域
– drawPlaceholderInRect:  
//重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– borderRectForBounds:  
//重写来重置边缘区域
– editingRectForBounds:  
//重写来重置编辑区域
– clearButtonRectForBounds:  
//重写来重置clearButton位置,改变size可能导致button的图片失真
– leftViewRectForBounds:
– rightViewRectForBounds:
 
委托方法
 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{  
    
//返回一个BOOL值,指定是否循序文本字段开始编辑  
    
    return YES;  
}  
 
- (void)textFieldDidBeginEditing:(UITextField *)textField{  

 //开始编辑时触发,文本字段将成为first responder  
}  
 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{  

//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder  
   
//要想在用户结束编辑时阻止文本字段消失,可以返回NO  
   
//这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息  
   
    return NO;  
}  
 
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{  

//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。  
//这对于想要加入撤销选项的应用程序特别有用  
//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。     
//要防止文字被改变可以返回NO  
//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中  
 
     return YES;  
}  
 
- (BOOL)textFieldShouldClear:(UITextField *)textField{  
 
//返回一个BOOL值指明是否允许根据用户请求清除内容  
//可以设置在特定条件下才允许清除内容  
 
     return YES;  
}  
 
-(BOOL)textFieldShouldReturn:(UITextField *)textField{  
 
//返回一个BOOL值,指明是否允许在按下回车键时结束编辑  
 
//如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起[textField resignFirstResponder];
//查一下resign这个单词的意思就明白这个方法了  

     return YES;  
}
/** * 住所選択 * @param stateData 画面の状態データ */ @Composable private fun AddressSelect( stateData: JAFRSHO10010UiStateData, ) { // 住所選択 入庫先区分 Row( modifier = Modifier .height(66.dp) .fillMaxWidth() .padding(start = 20.dp, top = 10.dp) .background(MaterialTheme.colorScheme.tabContentSelectedText), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_nyuko_place_cls), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), isShowUnderline = false, isShowFilter = false, isRequired = true, isTextEdit = false ) JAFRSHO10010DropText( item = Item( key = stateData.lstboxAddressNyukoPlaceCls.selectCode ?: "", value = stateData.lstboxAddressNyukoPlaceCls.selectName ?: "" ), modifier = Modifier .width(179.dp) .height(46.dp) .padding(start = 10.dp) ) } // 住所選択 住所 Row( modifier = Modifier .height(125.dp) .fillMaxWidth() .padding(start = 20.dp), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_address), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), value = stateData.lblAddress ?: "", textModifier = Modifier .width(458.dp) .height(109.dp), isShowUnderline = false, maxDisplayLength = 80, isTextEdit = false ) } // 都道府県 Row( modifier = Modifier .height(66.dp) .fillMaxWidth() .padding(start = 20.dp) .background(MaterialTheme.colorScheme.tabContentSelectedText), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_todofuken), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), value = stateData.txtboxTodofuken ?: "", textModifier = Modifier .width(458.dp) .height(46.dp), isShowFilter = false, isRequired = true, activeEnabled = ActiveInactive.ACTIVE ) } // 市区町村 Row( modifier = Modifier .height(66.dp) .fillMaxWidth() .padding(start = 20.dp), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_shikugun), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), value = stateData.txtboxShikugun ?: "", textModifier = Modifier .width(458.dp) .height(46.dp), isRequired = true, activeEnabled = stateData.stateActive.isShikugunEnabled ) } // 大字 Row( modifier = Modifier .height(66.dp) .fillMaxWidth() .padding(start = 20.dp) .background(MaterialTheme.colorScheme.tabContentSelectedText), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_oaza), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), value = stateData.txtboxOaza ?: "", textModifier = Modifier .width(458.dp) .height(46.dp), activeEnabled = stateData.stateActive.isOazaEnabled ) } // 丁目 Row( modifier = Modifier .height(66.dp) .fillMaxWidth() .padding(start = 20.dp), horizontalArrangement = Arrangement.Start, verticalAlignment = Alignment.CenterVertically ) { JAFRSHO10010TextFeild( label = stringResource(R.string.label_chome), labelModifier = Modifier .width(190.dp) .height(50.dp) .padding(start = 10.dp), value = stateData.txtboxChome ?: "", textModifier = Modifier .width(458.dp) .height(46.dp), activeEnabled = stateData.stateActive.isChomeEnabled ) } /*********************************************************** * Copyright NEC Corporation 2025. All rights reserved * No permission to use, copy, modify and distribute this * software and its documentation for any purpose is granted. * This software is provided under applicable license * agreement only. * * システム名 : JAF RSシステム * サブシステム名 : 車業アプリ * クラス名 : JAFRSHO10010TextFeild * * @author NEC * ***********************************************************/ package jp.or.jaf.syg.feature.jafrsho10.jafrsho10010.components import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.layout.wrapContentWidth import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.text.style.TextAlign import jp.or.jaf.syg.core.designsystem.theme.FigmaDesign.dp import jp.or.jaf.syg.core.designsystem.theme.FigmaDesign.sp import jp.or.jaf.syg.core.designsystem.theme.font.LocalNotoSansJPFontFamily import jp.or.jaf.syg.core.designsystem.theme.jafTextColor import jp.or.jaf.syg.core.designsystem.theme.requiredMark import jp.or.jaf.syg.core.designsystem.theme.textFieldLabel import jp.or.jaf.syg.core.designsystem.theme.textFieldUnderLine import jp.or.jaf.syg.core.ui.components.JafRefrenceButton import jp.or.jaf.syg.domain.model.common.ActiveInactive /** * 指令確認画面_牽引・伴走開始ポップアップのカスタムテキストフィールドコンポーネント * * @param label ラベルテキスト * @param value テキスト値 * @param onValueChange 値変更時のコールバック * @param maxDisplayLength 最大文字数を表す * @param isRequired 必須ボタンを表示するかどうか * @param labelModifier labelスタイル * @param textModifier textModifier * @param isShowUnderline 下線を表示するかどうか * @param isSingleLine 単一行にするかどうか * @param underlineColor 下線の色 * @param textAlign 入力ボックスtextAlign * @param unitString 入力ボックス単位 * @param visualTransformation VisualTransformation * @param keyboardOptions KeyboardOptions * @param isTextClick text(click) * @param isTextEdit text(edit) * @param isShowFilter 変換アイコンを表示するかどうか * @param onFilterClick 変換アイコンクリック * @param onClick コールバックをクリック * @param content カスタムコンテンツ * */ @Composable fun JAFRSHO10010TextFeild( label: String = "", value: String = "", onValueChange: (String) -> Unit = {}, maxDisplayLength: Int = 20, isRequired: Boolean = false, labelModifier: Modifier = Modifier, textModifier: Modifier = Modifier, isShowUnderline: Boolean = true, isSingleLine: Boolean = true, underlineColor: Color = MaterialTheme.colorScheme.textFieldUnderLine, textAlign: TextAlign? = TextAlign.Center, unitString: String? = null, visualTransformation: VisualTransformation = VisualTransformation.None, keyboardOptions: KeyboardOptions = KeyboardOptions.Default, activeEnabled: ActiveInactive = ActiveInactive.ACTIVE, isTextClick: Boolean = false, isTextEdit: Boolean = true, isShowFilter: Boolean = true, onFilterClick: () -> Unit = {}, onClick: () -> Unit = {}, content: (@Composable () -> Unit)? = null ) { // 色の定義 val labelColor = MaterialTheme.colorScheme.textFieldLabel val textColor = MaterialTheme.colorScheme.jafTextColor // ステータス管理 var displayText by remember(value) { mutableStateOf(value) } // 焦点状態追跡 var isFocused by remember { mutableStateOf(false) } LaunchedEffect(isFocused) { if (!isFocused && displayText != value) { onValueChange(displayText) } } LaunchedEffect(value) { if (!isFocused) { displayText = value } } Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.width(240.dp).fillMaxHeight() ) { Text( text = label, modifier = labelModifier.wrapContentHeight(align = Alignment.CenterVertically), style = MaterialTheme.typography.bodyMedium.copy( color = labelColor, fontSize = 22.sp, fontWeight = FontWeight.Bold, lineHeight = 26.sp, fontFamily = LocalNotoSansJPFontFamily.current ) ) // マークが必要(赤の背景付き) if (isRequired) { Text( text = "必須", color = Color.White, fontSize = 16.sp, modifier = Modifier .padding(start = 4.dp) .background( color = MaterialTheme.colorScheme.requiredMark, shape = RoundedCornerShape(4.dp) ) .width(42.dp) .padding(horizontal = 4.dp, vertical = 2.dp) ) } if (content != null) { content() } if (isTextClick || isTextEdit) { Column( modifier = textModifier .clip(MaterialTheme.shapes.medium) ) { if (isTextClick) { Text( text = value.let { if (it.length > maxDisplayLength) { it.take(maxDisplayLength - 1) } else { it } }, fontSize = 22.sp, modifier = textModifier .clickable(enabled = activeEnabled == ActiveInactive.ACTIVE) { onClick() } .wrapContentHeight(Alignment.CenterVertically) .padding(horizontal = 10.dp), textAlign = textAlign, color = textColor ) } if (isTextEdit) { BasicTextField( value = displayText.let { if (it.length > maxDisplayLength) { it.take(maxDisplayLength - 1) } else { it } }, onValueChange = { newText -> displayText = newText }, textStyle = LocalTextStyle.current.merge( TextStyle( fontFamily = LocalNotoSansJPFontFamily.current, fontWeight = FontWeight.Medium, fontSize = 22.sp, lineHeight = 26.4.sp, letterSpacing = 22.sp * 0.02f, color = textColor ) ), enabled = activeEnabled == ActiveInactive.ACTIVE, cursorBrush = SolidColor(MaterialTheme.colorScheme.primary), visualTransformation = visualTransformation, keyboardOptions = keyboardOptions, singleLine = isSingleLine, modifier = textModifier .padding(start = 10.dp) .wrapContentHeight(align = Alignment.CenterVertically) .onFocusChanged { focusState -> val wasFocused = isFocused isFocused = focusState.isFocused if (wasFocused && !isFocused && displayText != value) { onValueChange(displayText) } }, ) } // ダイナミックカラー下線 if (isShowUnderline) { Spacer( modifier = Modifier .height(1.dp) .fillMaxWidth() .background(underlineColor) ) } } if (isShowFilter && displayText.length > maxDisplayLength) { JafRefrenceButton( modifier = Modifier .size(28.dp) .wrapContentWidth(Alignment.End) .wrapContentHeight(Alignment.Bottom), enabled = true, onClick = onFilterClick ) } if (unitString != null) { Text( text = unitString, modifier = labelModifier, style = MaterialTheme.typography.bodyMedium.copy( color = labelColor, fontSize = 22.sp ) ) } } } } 以上代码中为什么都道府県的JAFRSHO10010TextFeild,不能输入文字?不是设置了activeEnabled = ActiveInactive.ACTIVE吗?
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值