iOS Question How to add eye symbol for TextField

Nitin Joshi

Active Member
Licensed User
Longtime User
In my project TextField is used for password by enabling Password Mode. How can is add eye icon at corner of text field to view the password.
 

jkhazraji

Active Member
Licensed User
Longtime User
Place a textfield (txtPassword) and a small label (Label1) on the right side
B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 8
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    Private txtPassword As TextField
    Private Label1 As Label
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
     Label1.Text=Chr(0xF06E) ' open
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub




Private Sub Label1_Click
    txtPassword.PasswordMode=Not(txtPassword.PasswordMode)
    If  txtPassword.PasswordMode Then
        Label1.Text=Chr(0xF06E) ' open
    Else
        Label1.Text=Chr(0xF070)  ' closed
    End If
    
End Sub
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
i don't think the native text field supports this, but you can use the following 2 alternatives:
or B4XFloatTextField
 
Upvote 1

jkhazraji

Active Member
Licensed User
Longtime User
Using OBJC:
B4X:
#if OBJC

#import <UIKit/UIKit.h>
@interface UITextField (B4iPasswordToggle)
- (void)addPasswordToggleButtonWithEyeOpen:(NSString *)eyeOpen eyeClosed:(NSString *)eyeClosed;
@end

@implementation UITextField (B4iPasswordToggle)

- (void)addPasswordToggleButtonWithEyeOpen:(NSString *)eyeOpen eyeClosed:(NSString *)eyeClosed {
    UIButton *toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // Set images for normal (closed eye) and selected (open eye) states
    [toggleButton setImage:[UIImage imageNamed:eyeClosed] forState:UIControlStateNormal];
    [toggleButton setImage:[UIImage imageNamed:eyeOpen] forState:UIControlStateSelected];
    
    toggleButton.frame = CGRectMake(0, 0, 16, 16);
    [toggleButton addTarget:self action:@selector(b4i_togglePasswordVisibility:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightView = toggleButton;
    self.rightViewMode = UITextFieldViewModeAlways;
    self.secureTextEntry = YES; // Start with password hidden
}

- (void)b4i_togglePasswordVisibility:(UIButton *)sender {
    self.secureTextEntry = !self.secureTextEntry;
    sender.selected = !sender.selected;
}

@end
#End If
 
Upvote 0
Top