0

I want cursor at specific position. I show my requirement in image.

 let arbitraryValue: Int = 5
 if let newPosition = txtroutine.position(from: txtroutine.beginningOfDocument, offset: arbitraryValue) {

        txtroutine.selectedTextRange = txtroutine.textRange(from: newPosition, to: newPosition)
    }

my requirement

my default cursor position

hardik
  • 199
  • 1
  • 6
  • 21
  • Isn't that where the cursor usually starts? – Sweeper Apr 25 '17 at 06:35
  • i want start cursor at given green line in image but by default my cursor start at beginning point of textfield. – hardik Apr 25 '17 at 06:38
  • Can you show the default behaviour and the behaviour you want side by side? – Sweeper Apr 25 '17 at 06:40
  • How have you given the padding at the beginning? – Inder Kumar Rathore Apr 25 '17 at 06:54
  • no but i use swift and i added this line in viewdidload()- > " textField.leftView = UIView(frame: CGRectMake(0, 0, 12, 0)); " in swift 3. i try like that " txtroutine.leftView = UIView(0, 0, 12, 0) " but it show me error : cannot invoke initializer for type uiview with argument list of type int – hardik Apr 25 '17 at 06:56
  • @Sweeper see the default behavior image and please help me. – hardik Apr 25 '17 at 08:33

4 Answers4

0

you can use leftView property of UITextField to set position as per your requirement :

//add a 12pt padding to the textField

Swift 3.0 :

textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 12, height: 0))
textField.leftViewMode = .always

Try this , feel free to comment .

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • can you please do this " textField.leftView = UIView(frame: CGRectMake(0, 0, 12, 0)); " in swift 3. i try like that " txtroutine.leftView = UIView(0, 0, 12, 0) " but it show me error : cannot invoke initializer for type uiview with argument list of type int – hardik Apr 25 '17 at 06:46
  • wait , m updating in swift 3 – Shobhakar Tiwari Apr 25 '17 at 06:49
  • its work but when i increase width it affect to placeholder and placeholder move to right. i need cursor beside first word of placeholder. – hardik Apr 25 '17 at 07:18
  • provide space before that , for text this wil work properly – Shobhakar Tiwari Apr 25 '17 at 07:21
  • i added space before placeholder but when i give width value in leftview as per your code then my placeholder move to right as per given width. this should not good. so suggest me to solve this issue – hardik Apr 25 '17 at 07:25
  • why u giving 12 , provide as per your requirement – Shobhakar Tiwari Apr 25 '17 at 07:27
  • it obvious i given as per my requirement like 15 but see cursor move from left to right its good but why placeholder move to right ? see the image with green line where i want cusrsor. – hardik Apr 25 '17 at 07:33
0

I have something like this but my code is in obj-c hope you can make it in swift,

Get the current position of cursor

 - (NSInteger)cursorPosition
{
    UITextRange *selectedRange = self.selectedTextRange;
    UITextPosition *textPosition = selectedRange.start;
    return [self offsetFromPosition:self.beginningOfDocument toPosition:textPosition];
}

// set cursor at your specfic location

- (void)setCursorPosition:(NSInteger)position
{
    UITextPosition *textPosition = [self positionFromPosition:self.beginningOfDocument offset:position];
    [self setSelectedTextRange:[self textRangeFromPosition:textPosition toPosition:textPosition]];
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • let arbitraryValue: Int = 5 if let newPosition = txtroutine.position(from: txtroutine.beginningOfDocument, offset: arbitraryValue) { txtroutine.selectedTextRange = txtroutine.textRange(from: newPosition, to: newPosition) } - i use this above code but it does not affect – hardik Apr 26 '17 at 11:27
  • @hardik [check this out](http://stackoverflow.com/questions/34922331/getting-and-setting-cursor-position-of-uitextfield-and-uitextview-in-swift/34922332), it is same as i done but in swift. and it is working. – dahiya_boy Apr 28 '17 at 07:27
0

You can achieve this by overriding -textRectForBounds:. It will only change the inset of text i.e in your case it's cursor. You need to subclass UITextField for that.

class PaddedTextfield: UITextField {
var horizontalInsetValue: CGFloat = 0
var verticalInsetValue: CGFloat = 0

override func textRect(forBounds bounds: CGRect) -> CGRect {
  return bounds.insetBy(dx: horizontalInsetValue, dy: verticalInsetValue)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
   return bounds.insetBy(dx: horizontalInsetValue , dy: verticalInsetValue)
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
   return bounds.insetBy(dx: horizontalInsetValue, dy: verticalInsetValue)
}
}

You can use this textfield wherever you want.

  • i am beginner in iOS so, i dont know how to implement it. – hardik Apr 25 '17 at 13:29
  • Instead of using UITextfield for that field, you can use this PaddedTextfield. PaddedTextfield is inherited from UITextfield so all of other properties are available in it. It overrides behaviour of some rect to achieve cursor position. –  Apr 26 '17 at 05:19
0

my problem is solved by adding this simple line in viewdidload()

  self.txtroutine.layer.sublayerTransform = CATransform3DMakeTranslation(15, 0, 0);
hardik
  • 199
  • 1
  • 6
  • 21