Set UiTextField cursor on top Swift IOS - ios

I have a TextField in my storyboard with a height of 100 .
When I'm clicking the TextField the cursor is placed in center :
I would like to set the cursor position in the top left corner.
I tried :
let startPosition: UITextPosition = textField.beginningOfDocument
But it seems that the position is already at the beginning and when the height is extended the cursor stay in center.
How can it be done ?

Seems like you want a multiline UITextField. In that case you might want to take a look at UITextView, which should allow for what you want (docs)

Related

How to change constraint of textfield programmatically?

i have menu icon and text field. When textFieldDidBeginEditing method called, i want to hide menu icon and change textfield's position to safe area top. I have connected textfield's top with menu icon. So i need to add constant with safe area programmatically.
Thanks.
Take the outlet of heightConstraint of menu icon and set it zero. On textview endediting set it on its default position.
How it Works:
Before textView began editing
textView.top(w.r.t safearea) = textView.top(w.r.t menu button) + btnmenu.height + btnMenu.top (w.r.t safearea)
After textView began editing as btnmenu.height = 0
textView.top(w.r.t safearea) = textView.top(w.r.t menu button) + btnMenu.top (w.r.t safearea)
Diametric Explanation:
Left : Before textView began editing
Right : After textView began editing as btnmenu.height = 0
Note : You can take outlet of constraints of BtnMenu.top & BtnMenu.bottom and set them as per your need.
Hope now you will be cleared.

Swift 4 - Editing label frame.size.height with negative number

So I have a label that I've made in Xcode's storyboard which I want to later edit in my code. I want it to simulate something like a vertical bar so I am editing its height by doing:
answerE.frame.size.height = -200
The problem comes from the negative number, I want the label to "grow" up so the height has to be negative from its original position... I have the line in code in a simple action on button press, but each time the line is executed the label moves "up" and eventually after 3-4 clicks is out of the screen.
I just want to edit its height, what is the correct way and what am I doing wrong?
My exact line in code is:
label.frame.size.height = -CGFloat(Double(x)/Double(y) * (200))
If you have added the label in storyboard, why not you use constraints to get the result.
Add leading, trailing , bottom and fixed height constraint and connect IBOutlet to height constraint. Change the constant value of height constraint at the event which you want to perform.
If I'm right, you want the label to gain height, keeping the same bottom edge, but the top edge moving up.
In order to do this, you want to change the frame.origin.y as you change the frame.size.height at the same amount, as its placement (and so top edge) is determined by its origin. So maybe make it zero height, place it where you want it in storyboard, and then when you want it to 'grow' by x:
label.frame.size.height = label.frame.height + x
label.frame.origin.y = label.frame.origin.y - x

How can I check out-of-sight characters in UITextField?

I have a UITextField that shrinks and expands when user input text. If the textfield's width reach the screen width, I want it to be right-aligned so user can see the last input characters. In other circumstance, I want it to be left-aligned.
Because the textfield's maximum width is not exactly the same with screen, I need to find a way to check if it has characters out of visible area.
Is there anyway to achieve this?
You could try checking the width of the string that is in the field and comparing it to the width of the field itself. It would look something like this -
CGSize textSize = [self.field.text sizeWithAttributes:#{NSFontAttributeName:self.field.font}];
// If the text is larger than the field
if (textSize.width > self.field.bounds.size.width) {
// There is text that is not visible in the field.
}
This is fairly rough, but should get you close.

Is it possible to add an offset of text to the UITextView?

I have a book app. User choose a book and UITextView show it. Every book has a caption. I need to add 55px offset to the top of UITextView. I want that every caption has 55px offset. But I don't know how to do this?
1)My text height is 22px. I can add 2 or 3 empty line of text, but it doesn't suit me because I need 55px.
2)I can add contentOffset.y = 55. But I have a problem. contentOffset will be zero if an user scroll to top.
Have you tried using contentInset in UIScrollView?
textView.contentInset = UIEdgeInsetsMake(55.0, 0, 0, 0);

UITextView increment character to the left

I have a UITextView that has a fixed width and height. I pre-populate the entire textfield with blanks.
I would like to insert a character with the push of a button that will erase the last blank character, insert my string character and then place the cursor at the beginning of the newly inserted string. I am trying to achieve inserting special fonts right to left and bottom to top.
It is working with the first button push and on the second button push the new value is inserted in the correct position to the left, however, the cursor will not move to the left after the second button push, it remains to the right after the second string insert.
Here is my code...
-(IBAction)chartP:(id)sender {
NSRange currentRange = myChart.selectedRange;
if (currentRange.length == 0) {
currentRange.location--;
currentRange.length++;
}
myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange
withString:[NSString string]];
currentRange.length = 0;
myChart.selectedRange = currentRange;
myChart.text = [myChart.text stringByAppendingString:#"p"];
myChart.selectedRange = NSMakeRange(myChart.selectedRange.location -1, 0);
}
Can someone assist me with what I am missing here to continually increment to the left with my string inserts?
How about flipping the text area:
myChart.transform = CGAffineTransformMakeScale(-1,-1);
It sounds like you are trying to implement right-to-left text direction by faking it. Why not do the real thing?
Here's a question that covers the topic:
Change the UITextView Text Direction
If you need bottom-to-top entry, and you have the ability to use a custom font, perhaps you can apply a transformation to the UITextView and y-flip it. Look at the transform property of UIView. (Things like the text selection loupe may break, but it's worth a try.)

Resources