Sunday, June 2, 2013

UITextField borderStyle example in Objective C (iOS).


UITextField borderStyle

The border style used by the text field.

@property(nonatomic) UITextBorderStyle borderStyle

Discussion of [UITextField borderStyle]
The default value for this property is UITextBorderStyleNone. If the value is set to the UITextBorderStyleRoundedRect style, the custom background image associated with the text field is ignored.

UITextField borderStyle example.
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];

Example of [UITextField borderStyle].
- (void)layoutSubviews {
   UITextField *searchField;
   NSUInteger numViews = [self.subviews count];
   for(int i = 0; i < numViews; i++) {
      if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.subviews objectAtIndex:i];
      }
   }
   if(!(searchField == nil)) {
       searchField.textColor = [UIColor whiteColor];
       [searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];
       [searchField setBorderStyle:UITextBorderStyleNone];
   }

   [super layoutSubviews];
}

UITextField borderStyle example.
- (void)viewDidLoad
{
    // Determine some basic info
    int numberOfTextfields = 11;
    int textfieldHeight = 40;
    int textfieldWidth = 200;

    // Create the UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, textfieldWidth,numberOfTextfields*textfieldHeight)];
    scrollView.contentSize = CGSizeMake(numberOfTextfields*textfieldWidth, textfieldHeight);

    // Create all the textfields
    NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
                                  (NSUInteger)numberOfTextfields];
    for(int i = 0; i < numberOfTextfields; i++) {
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0,i*textfieldHeight,textfieldWidth,textfieldHeight)];
        field.borderStyle = UITextBorderStyleRoundedRect;
        [scrollView addSubview:field];
        [textfields addObject:field];
    }

    [self.view addSubview:scrollView];

    [super viewDidLoad];
}

End of UITextField borderStyle example article.