Swift UITextField padding top - ios

I use UITextField and I want padding top.
So... How to in UITextField text padding top?
Sorry for my English:/

If I am not wrong then you want translate your text inside the textfield by y. UITextField have property know as sublayerTransform which can be used to translate the text. For example, to give a text view translate x by 10 and y by 10 you would use this code:
let titleTextField = UITextField(frame: CGRectZero)
titleTextField.layer.sublayerTransform =
let shiftTextByX = 10
let shiftTextByY = 10
titleTextField.layer.sublayerTransform CATransform3DMakeTranslation(shiftTextByX, shiftTextByY, 0)

Related

Intrinsic content size of button with text only

I have a uibutton, i want to set text dynamically at run time with padding top: 10, bottom 10 and left: 10 and right: 10, once i set text button title cliped. Any one can provide solution.
You must be attentive to fontSize of the button. Setting text programmatically doesn't automatically fits the text. Following code will help you.
//Swift
let btnLabel = yourButton.titleLabel
btnLabel.adjustsFontSizeToFitWidth = true
//Objective C
UILabel *btnLabel = yourButton.titleLabel;
btnLabel.adjustsFontSizeToFitWidth = true
Happy Coding <3

UILabel on second line if the content doesn't fit in the view's first line

The best way to describe my situations is with images. What I have is a view which contains several UILabels and UIImage. The red box is a UILabel and if the content is too big it should go to the second line.
From the storyboard I have a working case when the content fits but the problem is that I am not sure how to handle the case when the last (red box) should go to the second line. I am using autolayout and cartography.
If someone can point me to the right direction I will be very grateful.
First calcululate width of text as per your current label's position.
If text width is more than current label's width then see my answer from below link:
Auto Layout how to move the view2 from right to the bottom?
Calculate width:
func widthForView1(_ text:String, font:UIFont, height:CGFloat) -> CGFloat
{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: your_label_width, height: your_lable_height))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.text = text
label.font = font
label.sizeToFit()
return label.frame.width
}
You cannot do that with constraints only. To change the entire position of the element on the screen, you need to do it programmatically.
Use of tag View can solve this issue. TagListView is an external library.
When u add a view as subclass of taglistView, its height automatically increases accordingly.
ADD this to pod file : pod 'TagListView'
func addTags() {
let str1 = "Hi"
tagListView.addTag(str1)
let str2 = "Helloo"
tagListView.addTag(str2)
let str3 = "How Are u ? "
tagListView.addTag(str2)
tagListView.isUserInteractionEnabled = false
}

ios autolayout: dynamic width should push down 2nd label with SnapKit, proper way

I have 2 UILabels. lbl1's text can change, while lbl2's text is static.
They are both initially set on the same line. I have lbl1's numberOfLines set to 0 (infinity). I would like lbl2 to push down and align its top to lbl1's bottom when lbl1's width encroaches on lbl2's space. I have a current solution, but feel there's a pure autolayout way I'm missing. Any help would be great!
Both labels fit on 1 line:
lbl2 does not shift down in my attempts at pure autolayout:
Desired:
Current solution works, but feels un-ideal:
import XCPlayground
import SnapKit
import UIKit
let screen: UIView = UIView(frame: CGRectMake(0,0,320,568))
XCPlaygroundPage.currentPage.liveView = screen
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let lbl1: UILabel = UILabel()
lbl1.text = "Short Text asnoetuha 1 1 1 1 "
lbl1.numberOfLines = 0
lbl1.backgroundColor = UIColor.whiteColor()
let lbl2: UILabel = UILabel()
lbl2.text = "Amount Text"
lbl2.backgroundColor = UIColor.yellowColor().colorWithAlphaComponent(0.5)
screen.addSubview(lbl1)
screen.addSubview(lbl2)
lbl1.snp_makeConstraints { (make: ConstraintMaker) in
make.top.leading.equalTo(screen)
make.width.lessThanOrEqualTo(screen.snp_width)
}
lbl2.snp_makeConstraints { (make: ConstraintMaker) in
// Below lines feel un-ideal
let lbl2X: CGFloat = screen.frame.width - lbl2.intrinsicContentSize().width
let lbl1RightX: CGFloat = screen.frame.origin.x + lbl1.intrinsicContentSize().width
let hasOverlap: Bool = lbl1RightX >= lbl2X
make.top.equalTo(hasOverlap ? lbl1.snp_bottom : lbl1.snp_top).priorityLow()
make.trailing.equalTo(screen)
}
I do believe you are not approaching this the right way. You should be able to create a function that listens for when lbl1 has its right anchor come into contact with lbl2's left anchor (very easy with SnapKit).
When this does happen, you call a function that remakes the constraints to how fit the image you provided. This is done with a call to snap_updateConstraints or snp_remakeConstraints. Then your constraint code should be straight forward as you would just pin the bottom anchor of lbl1 to the top anchor of lbl2 and then just make the other constraints until you get that desired effect.

Swift 2 - Cant Loop UIView with Containing UITextField and Label

I am using swiftyjson to grab some json data and display it in a message box UITextView and UILabel. I have a for LOOP but i can not get the contents to loop inside of my UIScrollview.
I am creating a message box that displays username, date and message inside of a UIView.
My code:
for (_, subJson) in json["messageData"] {
self.outterMessageBox.hidden = false
self.usernameBox.text = subJson["user"].string
self.messageTXTBOX?.text = subJson["message"].string
self.dateBox.text = subJson["datetime"].string
}
And this is the output. Looks fine, just will not loop inside of my scroll view. I've also tried to programmatically create these views with the below code... It still does not loop. What can I be doing wrong?
Programmatically Created Views (i've displays only one iteration of the message box and does not loop as expected).
let myField: UITextField = UITextField (frame: self.ms.bounds)
myField.text = subJson["message"].string! + "\n"
self.ms.addSubview(myField)
let myNameLabel: UILabel = UILabel (frame: self.nm.bounds);
myNameLabel.text = subJson["user"].string
self.nm.addSubview(myNameLabel)
let myDt: UILabel = UILabel (frame: self.dt.bounds)
myDt.text = subJson["datetime"].string
self.dt.addSubview(myDt)
Any help with this would be greatly appreciated.

Adding multiple Lines of text into UILabel in swift

Hi I want to display some text in my UILabel line by line when an action is occurred
Hi, I have a textbox, button and a UILabel, lets say I've typed in
"This text will be displayed line by line in a UILabel"
and pressed the button to display the text in the UILabel..
so inside the UILabel it will look like:
This
text
will
be
displayed
line
by
line
in
a
UILabel
....
I've been searching forums but could not found an answer.. so I don't know if this is possible.. please help
p.s I've set number of lines to 0 and line breaks is set to word wrap
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 300)) // make the height biggest
// label.lineBreakMode = NSLineBreakMode.ByWordWrapping
// label.numberOfLines = 0
dont work if label width bigger word width
var string = "This text will be displayed line by line in a UILabel"
var array = string.componentsSeparatedByString(" ")
var newstring = join("\r", array)
label.text = newstring
let labelText = "Write\rthe\rtext\rwith\rline\rbreaks."
In order to wrap your text within a UILabel on Xcode 6 and above
Select the label you are working with.
Navigate to the Utilities panel.
Select the "Attributes inspector"
Select the number of lines you would like to display on the label (2,3,4) depending on the size of your UILabel field
Select "Word Wrap" under Line Breaks, or you can select any option of your preference.
You may manipulate the way your text appears to your taste.
Does it have to be a UILabel?
You could use UITextField instead which has some advantageous handling options

Resources