Resize textField Based On Content
You have to use an UITextView
instead of an UITextField
.
Then, you can use the sizeThatFits
method.
But first you have to know how high one line will be. You can get that information by using lineHeight
:
var amountOfLinesToBeShown: CGFloat = 6
var maxHeight: CGFloat = yourTextview.font.lineHeight * amountOfLinesToBeShown
After that, just call the sizeThatFits
method inside your viewDidLoad
method and set the maxHeight (line * 6
) as your textview height:
yourTextview.sizeThatFits(CGSizeMake(yourTextview.frame.size.width, maxHeight))
Textarea to resize based on content length
You can check the content's height by setting to 1px
and then reading the scrollHeight
property:
function textAreaAdjust(element) {
element.style.height = "1px";
element.style.height = (25+element.scrollHeight)+"px";
}
<textarea onkeyup="textAreaAdjust(this)" style="overflow:hidden"></textarea>
iOS UITextField Auto Resize conform to the content
With a UITextField, text must fit on one line and cannot wrap.
You have two options:
Shrink the font to fit on one line:
self.TextFieldExample.adjustsFontSizeToFitWidth = YES;
self.TextFieldExample.minimumFontSize = 10.0; //Optionally specify min size
Use UITextView to enable text wrapping:
See this answer: How to create a multiline UITextfield?
p.s. Per the style guide, "Properties should be camel-case with the leading word being lowercase." so you should rename your var to self.textFieldExample
Related Topics
Nsdateformatter Milliseconds Bug
What's the Uiscrollview Contentinset Property For
How to Disable/Enable the Sleep Mode Programmatically in iOS
How to Apply Multiple Transforms in Swift
How to Swizzle a Class Method on iOS
Changed Project Name in Xcode Causing Naming Error
Urlsession.Datatask with Request Block Not Called in Background
Xcode 9 - Failed to Emit Precompiled Header
Wkwebview Content Loaded Function Never Get Called
iOS Open Youtube App with Query (Url Schemes)
How to Force a Hkquery to Load the Most Recent Steps Counts
How to Check If a File Exists in Documents Folder
Ios: Uibutton Resize According to Text Length
How Does Cellforrowatindexpath Work
Upload Image from iPhone to the Server Folder