Adding space in UITextField After image - ios

My problem is that I have created a textfield which has leftviewmode as an image and the placeholder is in somewhere middle of textfield where the text editing should get start when user starts typing. So how to get my text started from middle of textfield just where the placeholder is. I cannot set the leftviewmode in imageview because I already added image there. What should I do?
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"a_point30"]];
image.frame = CGRectMake(0, 0, image.image.size.width, image.image.size.height-10);
self.enterSource.leftViewMode = UITextFieldViewModeAlways; self.enterSource.leftView = image;
For example textediting should began from hello world placeholder.

You can do something like this,
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
imgView.frame = CGRectMake(0.0, 0.0, dropDown.image.size.width+10.0, dropDown.image.size.height);
imgView.contentMode = UIViewContentModeLeft;
textField.leftView = imgView;
textField.leftViewMode = UITextFieldViewModeAlways;

In Swift 2.2.1, it will be something like this:
let imageView = UIImageView(image: UIImage(named: "ImageName"))
imageView.frame = CGRectMake(0.0, 0.0, imageView.image!.size.width + 10, imageView.image!.size.height)
imageView.contentMode = UIViewContentMode.Left
textField.leftView = imageView
textField.leftViewMode = UITextFieldViewMode.Always

Related

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

Convert C to Swift: Add magnifying glass icon to UITextField

How can I add a magnifying glass icon to the left of a UITextField?
I found an answer to a similar question here but I'm having trouble converting it to swift.
The answer:
So, here's the code with the unicode character:
UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];
[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];
Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.
My code:
let searchIconView = UILabel()
// Doesn't work: searchIconView.text = NSString.init(UTF8String: "\xF0\x9F\x94\x8D")
searchIconView.sizeToFit()
searchTextField.leftView = searchIconView
searchTextField.leftViewMode = .Always
Do you have a specific reason for trying to add the icon as a UTF8String? If you have a magnifying glass icon as a PNG image, you can use the "leftView" property of UITextField like this;
let imageView = UIImageView()
let magnifyingGlassImage = UIImage(named: "magnifyingGlass")
imageView.image = magnifyingGlassImage
//arrange the frame according to your textfield height and image aspect
imageView.frame = CGRect(x: 0, y: 5, width: 45, height: 20)
imageView.contentMode = .ScaleAspectFit
txtField.leftViewMode = .Always
txtField.leftView = imageView
In Swift you can assign emoji characters directly
searchIconView.text = "🔍"
two simple approaches:
You can use XCode to add Unicode symbols in your code directly (Edit->Emoji&Symbols).
You can use something like that
searchIconView.text = NSString(unicodeScalarLiteral: "\u{D83D}") as String
(you have to use your character here)
Updated for Swift 5 and using the System magnifying glass:
let imageView = UIImageView()
let magnifyingGlassImage = UIImage(systemName: "magnifyingglass", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
imageView.image = magnifyingGlassImage
//arrange the frame according to your textfield height and image aspect
imageView.frame = CGRect(x: 0, y: 5, width: 45, height: 20)
imageView.contentMode = .scaleAspectFit
txtField.leftViewMode = .always
textField.leftView = imageView

Using UILabel as a mask for a UIView [duplicate]

I'm trying to add a UIView to a UILabel, so that the text is the view's mask, enabling me to do things like animated text backgrounds (much like the slide to unlock label on the lockscreen).
The way I was planning on doing it was using the mask property on the view's layer to mask it to the shape of the text. However, I cannot find a way to get the UILabel's text shape as a CALayer.
Is this even possible? I can only find solutions that override the -(void)drawRect: method in the UILabel, but this wouldn't give me much flexibility.
UIView added the maskView property in iOS 8.0. Now, just create a UILabel to use as a mask for a UIView:
Objective-C:
UILabel *label = [[UILabel alloc] initWithFrame:self.view.frame];
label.text = #"Label Text";
label.font = [UIFont systemFontOfSize:70];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
UIView* overlayView = [[UIView alloc] initWithFrame:self.view.frame];
overlayView.backgroundColor = [UIColor blueColor];
overlayView.maskView = label;
[self.view addSubview:overlayView];
Swift 2:
let label = UILabel.init(frame: view.frame)
label.text = "Label Text"
label.font = UIFont.systemFontOfSize(70)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
let overlayView = UIView.init(frame: view.frame)
overlayView.backgroundColor = UIColor.blueColor()
overlayView.maskView = label
view.addSubview(overlayView)
This creates a crisp UILabel with UIColor.blueColor() color taken from overlayView.
mopsled's solution is more flexible if you building for iOS 8+. However, if you're looking for a pre-iOS 8 answer, here it is.
Thanks to Linuxios for pointing me to this question. The key is to use CATextLayer instead of UILabel.
Objective-C:
CGRect textRect = {0, 100, self.view.frame.size.width, 100}; // rect to display the view in
CATextLayer *textMask = [CATextLayer layer];
textMask.contentsScale = [UIScreen mainScreen].scale; // sets the layer's scale to the main screen scale
textMask.frame = (CGRect){CGPointZero, textRect.size};
textMask.foregroundColor = [UIColor whiteColor].CGColor; // an opaque color so that the mask covers the text
textMask.string = #"Text Mask"; // your text here
textMask.font = (__bridge CFTypeRef _Nullable)([UIFont systemFontOfSize:30]); // your font here
textMask.alignmentMode = kCAAlignmentCenter; // centered text
UIView* view = [[UIView alloc] initWithFrame:textRect];
view.backgroundColor = [UIColor blueColor];
view.layer.mask = textMask; // mask the view to the textMask
[self.view addSubview:view];
Swift:
let textRect = CGRect(x: 0, y: 100, width: view.frame.size.width, height: 100) // rect to display the view in
let textMask = CATextLayer()
textMask.contentsScale = UIScreen.mainScreen().scale // sets the layer's scale to the main screen scale
textMask.frame = CGRect(origin: CGPointZero, size: textRect.size)
textMask.foregroundColor = UIColor.whiteColor().CGColor // an opaque color so that the mask covers the text
textMask.string = "Text Mask" // your text here
textMask.font = UIFont.systemFontOfSize(30) // your font here
textMask.alignmentMode = kCAAlignmentCenter // centered text
let bgView = UIView(frame: textRect)
bgView.backgroundColor = UIColor.blueColor()
bgView.layer.mask = textMask // mask the view to the textMask
view.addSubview(bgView)
I create the custom maskLabel.
Check the answer
https://stackoverflow.com/a/47105664/7371852
This is the result.

Display UIView with a UILabel text mask

I'm trying to add a UIView to a UILabel, so that the text is the view's mask, enabling me to do things like animated text backgrounds (much like the slide to unlock label on the lockscreen).
The way I was planning on doing it was using the mask property on the view's layer to mask it to the shape of the text. However, I cannot find a way to get the UILabel's text shape as a CALayer.
Is this even possible? I can only find solutions that override the -(void)drawRect: method in the UILabel, but this wouldn't give me much flexibility.
UIView added the maskView property in iOS 8.0. Now, just create a UILabel to use as a mask for a UIView:
Objective-C:
UILabel *label = [[UILabel alloc] initWithFrame:self.view.frame];
label.text = #"Label Text";
label.font = [UIFont systemFontOfSize:70];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
UIView* overlayView = [[UIView alloc] initWithFrame:self.view.frame];
overlayView.backgroundColor = [UIColor blueColor];
overlayView.maskView = label;
[self.view addSubview:overlayView];
Swift 2:
let label = UILabel.init(frame: view.frame)
label.text = "Label Text"
label.font = UIFont.systemFontOfSize(70)
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
let overlayView = UIView.init(frame: view.frame)
overlayView.backgroundColor = UIColor.blueColor()
overlayView.maskView = label
view.addSubview(overlayView)
This creates a crisp UILabel with UIColor.blueColor() color taken from overlayView.
mopsled's solution is more flexible if you building for iOS 8+. However, if you're looking for a pre-iOS 8 answer, here it is.
Thanks to Linuxios for pointing me to this question. The key is to use CATextLayer instead of UILabel.
Objective-C:
CGRect textRect = {0, 100, self.view.frame.size.width, 100}; // rect to display the view in
CATextLayer *textMask = [CATextLayer layer];
textMask.contentsScale = [UIScreen mainScreen].scale; // sets the layer's scale to the main screen scale
textMask.frame = (CGRect){CGPointZero, textRect.size};
textMask.foregroundColor = [UIColor whiteColor].CGColor; // an opaque color so that the mask covers the text
textMask.string = #"Text Mask"; // your text here
textMask.font = (__bridge CFTypeRef _Nullable)([UIFont systemFontOfSize:30]); // your font here
textMask.alignmentMode = kCAAlignmentCenter; // centered text
UIView* view = [[UIView alloc] initWithFrame:textRect];
view.backgroundColor = [UIColor blueColor];
view.layer.mask = textMask; // mask the view to the textMask
[self.view addSubview:view];
Swift:
let textRect = CGRect(x: 0, y: 100, width: view.frame.size.width, height: 100) // rect to display the view in
let textMask = CATextLayer()
textMask.contentsScale = UIScreen.mainScreen().scale // sets the layer's scale to the main screen scale
textMask.frame = CGRect(origin: CGPointZero, size: textRect.size)
textMask.foregroundColor = UIColor.whiteColor().CGColor // an opaque color so that the mask covers the text
textMask.string = "Text Mask" // your text here
textMask.font = UIFont.systemFontOfSize(30) // your font here
textMask.alignmentMode = kCAAlignmentCenter // centered text
let bgView = UIView(frame: textRect)
bgView.backgroundColor = UIColor.blueColor()
bgView.layer.mask = textMask // mask the view to the textMask
view.addSubview(bgView)
I create the custom maskLabel.
Check the answer
https://stackoverflow.com/a/47105664/7371852
This is the result.

Enter a rightview UIImageView to a UITextField

I have a target action that when a button is pressed, I validate the UITextField:
// Get the text from the UITextField
NSString *nameStr = _name.text;
_name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"error.png"]];
[self.view addSubview:_name];
if (nameStr.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Name"
message:#"You must enter a name."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
My UITextField are added to the view in viewDidLoad like so:
[self.view addSubview:_name];
How do I make a rightView UIImageView appear?
You have to set rightViewMode of the Textfield because The default value for this property is UITextFieldViewModeNever.
so You have to set Mode from following,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
Are you sure you are not missing the UITextField frame ? And are you setting the rightViewMode ?
This is an example i just wrote seem to be working fine.
UITextField *rightField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 140, 50)];
rightField.backgroundColor = [UIColor redColor]; // For testing purpose
rightField.rightViewMode = UITextFieldViewModeAlways;// Set rightview mode
UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Tick_white_color"]];
rightField.rightView = rightImageView; // Set right view as image view
[self.view addSubview:rightField];
Here is the tick image as result :
For Swift 3.To show image before placeholder text in UITextField
func setPaddingView(strImgname: String,txtField: UITextField){
let imageView = UIImageView(image: UIImage(named: strImgname))
imageView.frame = CGRect(x: 0, y: 5, width: imageView.image!.size.width , height: imageView.image!.size.height)
let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
paddingView.addSubview(imageView)
txtField.leftViewMode = .always
txtField.leftView = paddingView
}
To call Function
self.setPaddingView(strImgname: "mail", txtField: txtEmail)
Solution in Swift 3:
class CustomText: UITextField {
#IBInspectable var rightImage : UIImage?{
didSet{
self.rightView = UIImageView(image: rightImage)
// select mode -> .never .whileEditing .unlessEditing .always
self.rightViewMode = .always
}
}
}
Solution with #IBDesignable and Swift 3
Firstly, you can create #IBDesignable for UITextField like this.
#IBDesignable extension UITextField{
#IBInspectable var setImageName: String {
get{
return ""
}
set{
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
self.rightView = imageView
self.rightViewMode = UITextFieldViewMode.always
}
}
Note: You must be write get and set points for #IBInspactable block because Swift has warn to apply getter and setter together. We've not need to use the get statement, so returned empty string. Also the attribute name has include Button but not confuse, its a mistake.
Then click the UITextField on StoryBoard and give the spesific image name to the setAButtonImageName attribute.
Then you will get the following result
The other situation is the create rightView with seems like has right margin
Here is the screenshot.
You can use a UIView as container than can add the UIImageView into the this UIView. In here, you must calculate the right margin, container view's width and `UIImageViews' width.
#IBInspectable var setImageName: String {
get{
return ""
}
set{
let containerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width:50, height: self.frame.height))
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
containerView.addSubview(imageView)
imageView.center = containerView.center
self.rightView = containerView
self.rightViewMode = UITextFieldViewMode.always
}
}
If you have two or more field then try this code! its working for me.
dropDownImageViewForTextField1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField1.image = [UIImage imageNamed:#"dropdown_n_icon.png"];
dropDownImageViewForTextField2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField2.image = [UIImage imageNamed:#"dropdown_n_icon.png"];
txtField1.rightViewMode = UITextFieldViewModeAlways;
txtField1.rightView = dropDownImageViewForTextField1;
txtField2.rightViewMode = UITextFieldViewModeAlways;
txtField2.rightView = dropDownImageViewForTextField2;
Do check 2 things
txtField.rightViewMode = UITextFieldViewModeAlways
Separate imageView for each textField

Resources