how to add a background image to a UITextView - ios

I am trying to add a background image to my UITextView, but there doesn't seem to be an option to do so. I have tried adding an imageView with the image as a sub view
self.bioTextView.delegate = self
let imgView = UIImageView(frame: self.bioTextView.frame)
imgView.image = #imageLiteral(resourceName: "text_field_bio")
self.bioTextView.addSubview(imgView)
But this didn't work either. The color of the textView Background is clear. I have also tried including sending that imgView to the back, but that didn't work either.

add the foll0wing two lines and check once
bioTextView.addSubview(imgView)
bioTextView.sendSubview(toBack: imgView)
or use
bioTextView.backgroundColor = UIColor(patternImage: UIImage(named: "Image.png"))
updated
let textView = UITextView(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat(400), height: CGFloat(400 )))
textView.text = "this is a test \n this is test \n this is a test"
let img = UIImageView(frame: textView.bounds)
img.image = UIImage(named: "1.jpg")
textView.backgroundColor = UIColor.clear
textView.addSubview( img)
self.view.addSubview(textView)
textView.sendSubview(toBack: img)
you get the output

Related

Material design Outline textfield with white background

Im trying to use material components library and make a textfield with outline and background color, but this is what I get
The white is outside the bounds.
Can you help me?
this is my code
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: "user")
txtfieldEmail.placeholderLabel.textColor = UIColor.gray
txtfieldEmail.leftView = imageView
txtfieldEmail.leftViewMode = .always
//txtfieldEmail.backgroundColor = UIColor.white
//txtfieldEmail.translatesAutoresizingMaskIntoConstraints = false
//txtfieldEmail.clipsToBounds = true
txtfieldEmail.layer.backgroundColor = UIColor.white.cgColor
usernameTextFieldController = MDCTextInputControllerOutlined(textInput: txtfieldEmail)
After I kept editing the form and adding more content, I ended up finding that we should be defining the style in the MDCTextInputControllerOutlined
So all I had to do was usernameTextFieldController?.borderFillColor = UIColor.white

Setting content mode in UIImageView no longer works in iOS 11

I have this code in order to put an UIImageView in the center of a navigation controller bar and properly scale the image:
override func viewDidAppear(_ animated: Bool) {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 28));
imageView.contentMode = .scaleAspectFit;
let image = UIImage(named: "image_title.png");
imageView.image = image;
self.navigationItem.titleView = imageView;
}
The code works fine in iOS 10, however in iOS 11 the ".scaleAspectFit" property is not considered and the images is not scaled in the UIImageView size.
I tried with some solutions i found:
Setting the frame of the UIImageView after setting the "contentMode" property
Setting imageView.setNeedsLayout()
Setting imageView.setNeedsDisplay()
unfortunately, no one of these solutions works. The "contentMode" property is simply ignored,
Any idea on what the problem could be?
Thank you in advance
Works for me such way (using additional UIView)
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 40))
let image = UIImageView(image: UIImage(named: "img-logo"))
image.contentMode = .scaleAspectFit
image.frame = titleView.bounds
titleView.addSubview(image)
viewController.navigationItem.titleView = titleView
let imageView = UIImageView(image: UIImage(named: "img-logo"))
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView

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

Swift how to make for navigation middle section image and text next to each other

I have searched a lot on stackoverflow and I havent been able to find solution for my problem which is that I need to put in navigationBar in middle section image and text next to each other but apparently I was able only to place just image or just text
Here is my code
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
self.navigationItem.title = "App name"
self.navigationItem.titleView = imageView
I am aware that it on this way it accept only image which is titleView but I cant think of some normal solution and dont know is it even possible. Also if that is not possible how can I make then blank image and draw that icon image and text next to each other and append then that new image.
Edit:
Based on awph answer I have added code like this and it works fine for my case where I have always same characters in title
let screenSize: CGRect = UIScreen.mainScreen().bounds
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: screenSize.width / 2 - 40, y: 15, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
nav!.addSubview(imageView)
// label on top of the application that contain word
var label = UILabel(frame: CGRectMake(0, 0, 0, 0))
label.center = CGPointMake(screenSize.width / 2 - 15, 17)
label.textColor = UIColor.blackColor()
label.font = UIFont.systemFontOfSize(14)
label.textAlignment = NSTextAlignment.Center
label.text = "App Name"
label.sizeToFit()
nav!.addSubview(label)
You right, as said in documentation:
// Custom view to use in lieu of a title. May be sized horizontally. Only used when item is topmost on the stack.
What you need to do is to create an UIView, add an UImageView (that contains your image) as subview (view.addSubview(imageView)) and then add a UILabel on it view.addSubview(label).

Resources