2011-04-26 362 views
25

如何以編程方式設置UITextField的邊框樣式?如何設置UITextfield的邊框樣式

我創造我的文本字段像這樣:

UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)]; 
tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];  
tfText.textAlignment = UITextAlignmentCenter; 
[self.view addSubview:tfText]; 
[tfText release]; 

回答

51

試試這個

UITextField *tfText = [[UITextField alloc] initWithFrame:CGRectMake(65, 200, 200, 30)]; 
tfText.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.5 alpha:0.3];  
tfText.textAlignment = UITextAlignmentCenter; 
// Border Style None 
[tfText setBorderStyle:UITextBorderStyleNone]; 
[self.view addSubview:tfText]; 
[tfText release]; 

僅供參考

8

您可以使用Quartzcore和圖層屬性。

sometextfield.layer.borderWidth = 1; 
sometextfield.layer.borderColor = [[UIColor redColor] CGColor]; 

我必須記住要將QuartzCore添加到您的項目並將其導入到您想要使用它的地方。

1
[pTxtTithiTime.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]]; 

[pTxtTithiTime.layer setBorderWidth:0.8]; 

//圓角部分,在那裏你指定你的觀點的角球弧度IUS:

pTxtTithiTime.layer.cornerRadius = 5; 

    pTxtTithiTime.clipsToBounds = YES; 
0

你可以的UITextField編程方式是這樣的:

UITextField *txtField = [[UITextField alloc]initWithFrame:CGRectMake(10, 260, 280, 30)]; // your required coordinate 
txtField.delegate =  self; 
txtField.placeholder =  @"My TextField"; 
txtField.borderStyle =  UITextBorderStyleNone; 
txtField.keyboardType =  UIKeyboardTypeDefault; 
txtField.backgroundColor =  [self setBackGroundColorOnButton]; 
txtField.layer.cornerRadius = 5.0; 
5

從以下四個任何人都可以通過編程的方式使用,

[textField setBorderStyle:UITextBorderStyleNone]; 

[textField setBorderStyle:UITextBorderStyleLine]; 

[textField setBorderStyle:UITextBorderStyleBezel]; 

[textField setBorderStyle:UITextBorderStyleRoundedRect]; 

其中文本框UITextField

2

我知道,問題是關於Obj-C,但我來到這裏是爲了Swift。而在斯威夫特,它的完成這樣的:

txt.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0); 
txt.borderStyle = UITextBorderStyle.RoundedRect 
let myColor : UIColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0); 
txt.layer.borderWidth = 3; 
txt.layer.borderColor = myColor.CGColor; 

這將設置一個自定義背景色,並在相同的顏色(隱藏邊框,但仍然有文本和文本框邊界之間一些填充邊界