UIButton's textLabel not being clipped - swift

I have a UIButton,and I set its text like this:
let myButton ...
myButton.textLabel?.text = "abcdefghijklmn"
But when I run it ,the text is clipped. How could UIButton auto resize to fit its inner text.
Here is the screenshot.
What I want is this:
abcdefghijklmn

Option A: myButton.sizeToFit()
Option B: use layout constraints that don't constrict the label's width

Related

How to resize uilabel inside stackview?

I have uilabel call "sklabel" inside stackview like image below. But sklabel width size is following stavkview size. How to make it's width follow text width?
Should i make something like Stackview -> uiview -> uilabel ?

UIImageView height same size as UILabel font height. Centered inside UIStackView

I'm trying to take a label, followed by an imageView with the same height as the label's font, centered inside of a stackView.
For whatever reason, I can't figure out how to get this inside of a stackView. Here's what I got.
If the font size of the label is fixed, the height can be obtained.
(statusLabel.text as! NSString).size(withAttributes: [NSAttributedString.Key.font : statusLabel.font]).height

Controls at the bottom of UIScrollView not working

The UIButton in the scrollView is visible, but not accessible. I am using Constraints.
My UI structure is this:
- UIView
- scrollView: UIScrollView
- contentView: UIView
- UIButton
- UIButton
- UIButton
- ....
- UIButton
I've already tried to set the contentSize of the scrollView. And the height of the contentView. Next to that I've tried to uncheck the checkbox Adjust Scroll View Insets in the storyboard of that ViewController without any luck. I've also set the priority of the Align Center Y to 250, and the priority bottom space to 250 of the contentView.
func updateScrollViewSize() {
var contentRect = CGRect.zero
for view in contentView.subviews {
contentRect = contentRect.union(view.frame)
}
contentRect.size = CGSize(width: scrollView.contentSize.width, height: contentRect.height + 50)
scrollView.contentSize = contentRect.size
contentView.frame.size = contentRect.size
contentView.layoutIfNeeded()
}
The button I try to reach has a Y value of: 1030.0
The height of the contentView is: 871.0
Step-by-step:
Add a scroll view to your view, background color red, constrain it 20-pts on each side:
Add a UIView as your "content view" to the scroll view (green background to make it easy to see), constrain it 0-pts on each side, constrain equal width and equal height to the scroll view. Important: change the Priority of the Height constraint to 250!
Add a UILabel to the contentView, constrain it 30-pts from the top, centered horizontally:
Add another label to the contentView, constrain it 300-pts from the first label, centered horizontally:
Add a UIButton to the contentView, constrain it 30-pts from the bottom, centered horizontally:
Now add a vertical constraint from the bottom of the second label to the top of the button, and set it to 400-pts:
As you see, this pushes the button off-screen past the bottom of the scroll view.
Run the app. You will be able to scroll down to see the button, and you'll be able to tap it.
Absolutely no code needed!
If you use AutoLayout, you don't needed to install frames manually.
You can try install constraints properly and content size will be right in this case and you won't have to install content size manually.
If you achieve this, I guess everything will work correctly.
You can follow this or this guide

Image alignment

I'm setting the Scaling and Alignment of an ImageView in the Attributes inspector.
But the Alignment property only works selecting a Border Type. I don't want to add any style.
I've tried this code:
override func viewDidAppear() {
logoImageView.layer?.contents = logoImageView.image
logoImageView.layer?.contentsGravity = kCAGravityResizeAspect
}
But the image is centered and I need it left aligned.
With kCAGravityLeft I get an error and it doesn't resize.
Misuse of NSImage and CALayer. contentsGravity is left. It should be
one of resize, resizeAspect, or resizeAspectFill.
Thanks in advance.

How to automatically adjust the width of a centered UITextfield in Swift while editing?

I have a custom UITextfield that is x-centered to its superview via a constraint. It also has a height-constraint and a nice thin line right under the text that has the same width as the textfield itself (like I said: custom).
I now want to keep the UITextfield centered while editing its content, adjusting itself in width to its string content.
What right now happens is that it keeps its width while editing, cropping its overlapping content.
My code is kind of
// on textfields editing change event
#IBAction func nameFieldEditingChanged(sender: AnyObject) {
// set width of textfield to width of textstring
checkUserNameTextfieldWidth()
}
func checkUserNameTextfieldWidth(){
println("checkUserNameTextfieldWidth \(userName.text) / \(userName.frame.size.width) / \(view.bounds.width)")
// something has to happen here I guess...
}
Thnx!
You have to use UITextView instead of UITextField and apply its sizeThatFits method (see this response)