Text padding on UILabel not working - ios

This post is to complete one I made a while ago titled Text padding on UILabel, left with no answer.
The screenshot below shows it all. The first label, though the shortest is truncated. The second and the third even more have far too wide spaces on the right side. I want to solve those 2 problems.
I made a tiny project and provide it on GitHub to simply illustrate the issu and give other people the opportunity to easily take a close look.

Please update your NSLayoutConstraint like this
view.addConstraints([
NSLayoutConstraint(item: label,
attribute: .left,
relatedBy: .lessThanOrEqual,
toItem: view,
attribute: .left,
multiplier: 1.0,
constant: sideMargin),
NSLayoutConstraint(item: label,
attribute: .right,
relatedBy: .lessThanOrEqual,
toItem: view,
attribute: .right,
multiplier: 1.0,
constant: sideMargin * -2),
NSLayoutConstraint(item: label,
attribute: .top,
relatedBy: .equal,
toItem: topView,
attribute: firstLoop ? .top : .bottom,
multiplier: 1.0,
constant: sideMargin),
NSLayoutConstraint(item: label,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])

Related

Mapbox iOS move attribution Button

I'm using the mapbox SDK and I need to show their logo watermark and their attribution mark. I can move them though, so I want to move the attribution button from the bottom right to bottom left next to the logo watermark.
I tried the following code but it did not work:
func mapViewWillStartLoadingMap(_ mapView: MGLMapView) {
mapView.attributionButton.frame.offsetBy(dx: -200.0, dy: 0)
}
This is how it looks like:
I tried solutions like Harshal's but couldn't get that to work. Here's what worked for me (placing the attribution button near upper left). (Code added to viewDidLoad())
self.view.addConstraints([
NSLayoutConstraint(item: mapView.attributionButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 25),
NSLayoutConstraint(item: mapView.attributionButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 25),
NSLayoutConstraint(item: mapView.attributionButton, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 72),
NSLayoutConstraint(item: mapView.attributionButton, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 24),
])
mapView.attributionButton.contentHorizontalAlignment = .left
mapView.attributionButton.frame.size = CGSize(width: self.mapView.frame.size.width - 120, height: 25)
you can also hide attribution Button as follow:
mapView.attributionButton.alpha = 0
Take a look at scaleBarPosition in MGLMapView
MapBox added the ability to do this in 4.3.0
https://github.com/mapbox/mapbox-gl-native/commit/60ceac5efc3d77199f773f08400fe1d53d5a1b90

NSLayoutConstraints breaking but seem fine

I'm trying to pin an image view to the top, left, and right of a superview, and give it a height with NSLayoutConstraint like so:
backgroundView.addSubview(imageView)
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: backgroundView, attribute: .Top, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Left, relatedBy: .Equal, toItem: backgroundView, attribute: .Left, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Right, relatedBy: .Equal, toItem: backgroundView, attribute: .Right, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.bounds.width))
But the image doesn't show up and the debugger is logging that my constraints are broken. I don't see what constraint could be causing the problem.
fixed by adding imageView.translatesAutoresizingMaskIntoConstraints = false before adding the imageView as a subview to backgroundView.

Move Ad Banner down below Status Bar using NSLayoutConstraint Swift

In Swift, I am trying to add a Google Smart Ad Banner using NSLayoutConstraints at the top of the view, but below the status bar by 14 points. I've been trying for ages now with various different attributes and top or topLayoutGuide.
Link to Image
let xConstraint = NSLayoutConstraint(item: bannerView, attribute: .centerX,
relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let pinTop = NSLayoutConstraint(item: bannerView, attribute: .top,
relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .top, multiplier: 1, constant: 14)
self.view.addSubview(bannerView)
self.view.addConstraint(xConstraint)
self.view.addConstraint(pinTop)
You’re pinning the banner to the top of the topLayoutGuide, but what you really want is the bottom of the topLayoutGuide, so that it will start below the status bar:
let pinTop = NSLayoutConstraint(item: bannerView,
attribute: .top,
relatedBy: .equal,
toItem: self.topLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 14)

UITextField autolayout with margins programmatically

I'm new to AutoLayout and would like to display my UITextField at 100% width with a consistent 15px left and right margin, like so:
Typically I would do this using CGRect, setting the width to the containing view's width minus 30px, then offset the left side by 15px:
searchTextField.frame = CGRectMake(15, 0, view.frame.width - 30, 50)
But I'd like to learn AutoLayout for this sort of thing, since it's the way to go these days. I should note that I am doing everything programmatically -- no Storyboards here.
I'd love it if someone could help me out!
Update
Wow! Thank you for all the responses. I believe all of them would achieve what I'm trying to do, but there can only be one :)
Usually I use for this cocoapod that is dealing with constraints, but if you need pure apple solution documentation states:
You have three choices when it comes to programmatically creating
constraints: You can use layout anchors, you can use the
NSLayoutConstraint class, or you can use the Visual Format Language.
Approach with NSLayoutConstraints in your case would be:
NSLayoutConstraint(item: textField, attribute: .Leading, relatedBy: .Equal, toItem: parentView, attribute: .LeadingMargin, multiplier: 1.0, constant: 15.0).active = true
NSLayoutConstraint(item: textField, attribute: .Trailing, relatedBy: .Equal, toItem: parentView, attribute: .TrailingMargin, multiplier: 1.0, constant: -15.0).active = true
NSLayoutConstraint(item: textField, attribute: .Top, relatedBy: .Equal, toItem: parentView, attribute: .TopMargin, multiplier: 1.0, constant: 50.0).active = true
Remember that if you don't have any constraints in the view, they would be added automatically and you'll have to deal with them and conflicts that would be created by adding new constraints on runtime. To avoid this you could either create textField manually and add it to the view or set constraints with low priority in the Interface Builder .
Assuming the parent of the text field is view, do this after view.addSubview(searchTextField):
NSLayoutConstraint.activateConstraints([
searchTextField.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor, constant: 15),
searchTextField.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -15),
])
Use this code:
let topConstraint = NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 15)
let leadingConstraint = NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 15)
let heightConstraint = NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50)
self.view.addConstraint(topConstraint )
self.view.addConstraint(trailingConstraint)
self.view.addConstraint(leadingConstraint)
self.view.addConstraint(heightConstraint)
Set the constraints in storyboard.
Click on the text field then click on in the bottom left. From there you can choose constraints like that.
To use Auto Layout, you need to define constraints for your text field.Here, I have created four constraints(Leading, Trailing, Top and Height) related to its superview.
func addLabelConstraints(superView:UIView) {
let leading = NSLayoutConstraint(item: searchTextField, attribute: .Leading, relatedBy: .Equal, toItem: superView, attribute: .Leading, multiplier: 1, constant: 15)
superview!.addConstraint(leading)
let trailing = NSLayoutConstraint(item: searchTextField, attribute: .Trailing, relatedBy: .Equal, toItem: superView, attribute: .Trailing, multiplier: 1, constant: 15)
superView.addConstraint(trailing)
let top = NSLayoutConstraint(item: searchTextField, attribute: .Top, relatedBy: .Equal, toItem: superView, attribute: .Top, multiplier: 1, constant: 0)
superView.addConstraint(top)
let height = NSLayoutConstraint(item: searchTextField, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 0, constant: 50)
superView.addConstraint(height)
}

Make UITableViewCell editable

Given the Detail View in Apple's tutorial, how do I make the cells in the detail view editable? Not just to add or delete cells, but to modify the text in the cell value (right label in the cell)?
in both your UI and your code, change the UILabel objects to UITextField, hook them up the same way. they will allow editing of text in those fields.
you will then also have to hook up your viewController to fetch them when the user changes them (via delegate setup) and set up your model to accept the data from your delegate implementation.
good luck.
There's a pod for that: https://github.com/fulldecent/FDTextFieldTableViewCell
Here's the relevant code:
self.detailTextLabel?.isHidden = true
self.contentView.viewWithTag(3)?.removeFromSuperview()
self.textField.tag = 3
self.textField.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(self.textField)
self.addConstraint(NSLayoutConstraint(item: self.textField, attribute: .leading, relatedBy: .equal, toItem: self.contentView, attribute: .leading, multiplier: 1, constant: 50))
self.addConstraint(NSLayoutConstraint(item: self.textField, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 8))
self.addConstraint(NSLayoutConstraint(item: self.textField, attribute: .bottom, relatedBy: .equal, toItem: self.contentView, attribute: .bottom, multiplier: 1, constant: -8))
self.addConstraint(NSLayoutConstraint(item: self.textField, attribute: .trailing, relatedBy: .equal, toItem: self.contentView, attribute: .trailing, multiplier: 1, constant: -16))
self.textField.textAlignment = .right

Resources