Is there a way to move the cursor of TextField to the end of the text when editing?- SwiftUI - swift

I have created a textfield in Swift UI and I'm looking for a way to make the cursor of the textfield on editing to be at the end of the text even if I tapped the first letter on the textfield.

I think you mean something like this, to set .endOfDocument:
let endPosition = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from: endPosition, to: endPosition)
Now it will stay at the end of the position. If you search something more advanced, I recommend you this post and its answer.

Related

how to set textfield size fix macos swift

I have one textfield "A" with width 150. but whenever I am passing more characters to textfield "A", it is getting overlapped on other textfield "B" characters.
I want textfield "A" to be fixed size and scrollable(mostly right and left).
Thanks in advance.
You should use Auto-Layeout in your storybook. Set fixed width, trailing space and leading space to your text field.
I have added the Auto-Layeouts to my textfields and I have followed below steps:
1) Selected the label.
2) Go to attributes inspector.
3) Select 'Line Breaks' and choose an option Truncate Tail
4) now I can see the text without overlapping with another textField.
Note: But still I am not able to view/Scoll the full text.
Just found this piece of code. It might help you
scrollView.hasHorizontalScroller = true
textView.maxSize = NSMakeSize(CGFloat(FLT_MAX), CGFloat(FLT_MAX))
textView.isHorizontallyResizable = true
textView.textContainer?.widthTracksTextView = false
textView.textContainer?.containerSize = NSMakeSize(CGFloat(FLT_MAX), CGFloat(FLT_MAX))

How to save and restore NSScrollView scroll position in Swift?

In a macOS project I'm trying to get and set the scroll cursor position of a NSTableView the code seems to work because don't appairs errors but the cursor don't update.
var clipOrigin = tableView.enclosingScrollView?.contentView.bounds.origin // get the position
clipOrigin = CGPoint (x: 0.0, y: 3.0) // set a new position
tableView.enclosingScrollView?.contentView.scroll(to: clipOrigin!) // set the new position
Am I forgetting something?
contentView.bounds.origin will almost certainly give you a point at 0,0 pretty much always as that's not the value you are expecting. bounds tell you an information about a position of the view which displays the canvas, not the inner offset of the scrolled document.
I guess one -documentRect or -documentVisibleRect properties contains the proper offset value you're looking for.

How to set UIButton titlelabel topdown

will like to know how to set the text in UIButton title Label in top down.
the text is "Press Me" across
will like to show
"
p
r
e
s
s
m
e
"
i did this
CGAffineTransform newTransform = CGAffineTransformMakeRotation(90 * (M_PI / 180));
self.activeButton.transform = newTransform;
but it only changed the button direction not the text
Rotating text to be vertical is different than writing each letter on a separate line, which is what I gathered to be your intention from your question.
To do that, you need to actually write each letter on a separate line! You can create new lines in UIButton text by holding Alt/Option while pressing enter in the text field in Interface Builder. Note that this has to be the text field property in the Utilities panel, you can't add new lines if you're editing the text by double clicking the button.
Once you've done this, change the "Line Break" mode to either Character Wrap or Word Wrap in order for it to display multiple lines.
Edit: I realised that you may be trying to work with your button in code, so I wrote this piece that should convert a regular button's text to be spaced vertically, letter by letter:
// Create a temporary NSString to store the new formatted text string
// Set it to the first character so we can have a simple loop from the second to the last character
NSString *newText = [NSString stringWithFormat:#"%C",[button.titleLabel.text characterAtIndex:0]];
for(int i=1;i<button.titleLabel.text.length;i++) {
// Format newText to include a newline and then the next character of the original string
newText = [NSString stringWithFormat:#"%#\n%C",newText,[button.titleLabel.text characterAtIndex:i]];
}
// We must change the word wrap mode of the button in order for text to display across multiple lines.
button.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
// .. and for an unknown reason, the text alignment needs to be reset. Replace this if you use something other than center alignment.
button.titleLabel.textAlignment = NSTextAlignmentCenter;
// newText now contains the properly formatted text string, so we can set this as the button label
[button setTitle:newText forState:UIControlStateNormal];

If using Xcode to make an iOS calculator, how would I add a decimal button?

How to add a decimal button so I don't have to do whole numbers? If i wanted any decimal number like 1.2 or 100.4 or 3.0 or anything like that, how would I add it to the calculator I'm making?
You're not giving me much information in your question. How do the other buttons work?
If I was making a calculator I would have a label at the top showing the current reading. On press of a number button I would update the label with the number at the end.
For a decimal button you just add a . to the end of the label. You might want to have a global variable BOOL hasDecimalPlace and set it to true so you know if there is already a decimal place. Just remember to set it to false again when you clear the view or do a calculation or similar.
- (IBAction)Decimal:(id)sender{
NSString *currentText = Text.text;
if ([currentText rangeOfString:#"." options:NSBackwardsSearch].length == 0) {
Text.text = [Text.text stringByAppendingString:#"."];
}
}

How to wrap a line in label in iphone?

I am new to iphone development .I want to wrap up the text in the label.I want to wrap up to a particular position in the label.Thanks.
theLabel.lineBreakMode = UILineBreakModeWordWrap;
theLabel.numberOfLines = 0;
That'll let it wrap an arbitrary number of lines. If you want to limit them, and have it truncate with a “...”, then set the numberOfLines property to that value.
Set "Line breaks" in Interface Builder ( http://drp.ly/d3K65 ) to "Word Wrap" and increase height of the UILabel.
You can do the same thing (or even more complex) in the code, see UILabel reference http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html