swift 3 ios : UILabel - ios

I'm trying to create a UILabel, with dynamic height
this is my code :
label1.text = Message
label1.textColor = UIColor.white
let font = UIFont.systemFont(ofSize: 17.0)
label1.font = font
label1.textAlignment = .center
label1.lineBreakMode = NSLineBreakMode.byWordWrapping;
label1.numberOfLines = 0;
label1.sizeToFit()
label1.frame = CGRect(x: 0, y:40 , width: 270, height: label1.frame.height)
however, my label stay with one line, where my Message is about 3 lines
so what could be the best solution for this problem?

The best solution would be using constraints.
Either directly in the InterfaceBuilder or via code.
(SnapKit would be a good and easy way to build constraints via code)

Try
let size = label1.sizeThatFits(CGSize(width: 270, height: 100000))
label1.frame = CGRect(x: 0, y: 40, width: 270, height: size.height)
for the last two line of your code.

you can do it with storyboard
label height relation to greater than or equal

Related

Reduce gap between left-view and curser in textfield

I am using below code for adding left view in textfield:
func setUI()
{
txtUserCode.leftViewMode = .always
let label = UILabel(frame: CGRect(x: 7, y: 0, width: 18, height: txtUserCode.frame.size.height))
label.text = "#"
txtUserCode.leftView?.frame = CGRect(x: 0, y: 0, width: 18, height: txtUserCode.frame.size.height)
txtUserCode.leftView = label
label.backgroundColor = UIColor.green
txtUserCode.leftView?.backgroundColor = UIColor.red
}
But the problem is there is gap between left view and curser you can see in screen shot.
See there is small gap between curser and the left view, you can see between 'premB' and '#'
Any help or suggestion will be helpful.
Update your code to below.
func setUI()
{
txtUserCode.leftViewMode = .always
let label = UILabel(frame: CGRect(x: 7, y: 0, width: 18, height: txtUserCode.frame.size.height))
label.text = "#"
label.sizeToFit()
label.frame = CGRect(x: 7, y: 0, width: label.frame.size.width, height: txtUserCode.frame.size.height)
txtUserCode.leftView?.frame = CGRect(x: 0, y: 0, width: label.frame.size.width, height: txtUserCode.frame.size.height)
txtUserCode.leftView = label
label.backgroundColor = UIColor.green
txtUserCode.leftView?.backgroundColor = UIColor.red
}
To get the exact size, first make sizeToFit for the label & instead of 18, give the width of the label.
Add below line and try again.
txtUserCode.borderStyle = .none

How to calculate size of UILabel according to the text in Swift

I need to adjust the size of the UILabel according to its size dynamically, width may be constant but height should be dynamic
let label:UILabel = UILabel(frame: CGRect(x: 30, y: 0, width: curWidth!, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = curFont!
label.sizeToFit();

Adjust label based on Text given in Text view in swift 3?

I am working on Photo Editing App and i am trying to adjust label Based on Text given in Text View . I have tried Lots of code but it did not work for me. i want to add Label on Image Which is based on text given in my textview as shown in Image given below. I am unable to set width and height of label based on textview's text. I have Tried Following code. Some one please help.
let newWidth: CGFloat = CGFloat(txtView.text.boundingRect(with: txtView.frame.size, options: .usesLineFragmentOrigin, context: nil).size.width)
let Label = UILabel(frame: CGRect(x: 100, y: 100, width: newWidth, height: 100))
imgView?.addSubview(Label)
I have also tried
var maximumLabelSize = CGSize(width: 296, height: FLT_MAX)
var expectedLabelSize: CGSize = yourString.size(withFont: yourLabel.font, constrainedTo: maximumLabelSize, lineBreakMode: yourLabel.lineBreakMode)
//adjust the label the the new height.
var newFrame: CGRect = yourLabel.frame
newFrame.size.height = expectedLabelSize.height
yourLabel.frame = newFrame
I have solved my issue using JLStickerTextView but in this repository its not compatible for Swift 3.0 so i have made changes in project and you can find out Working Project without any errors from her https://github.com/khush004/StickerView/tree/master
func calcheight(strin:String) -> CGFloat
{
let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.text = strin
label.sizeToFit()
return label.frame.height + 2
}

How do I add two UILabel to a single TableView?

I'm making a message when the TableView is empty, this is code :
let emptyLabel=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
let emptyLabel2=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
emptyLabel.text = "no reminder added yet"
emptyLabel.textColor=UIColor.darkGray
emptyLabel.font=UIFont(name: "HelveticaNeue-Light", size: 18)
emptyLabel.textAlignment = NSTextAlignment.center
emptyLabel2.text = "you add by going back to the homescreen"
emptyLabel2.textColor=UIColor.darkGray
emptyLabel2.font=UIFont(name: "HelveticaNeue-Light", size: 12)
emptyLabel2.textAlignment = NSTextAlignment.center
self.tableView.backgroundView = emptyLabel2
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
return 0
It works, but how do I add emptyLabel2 beneath the emptyLabel. I want to use the defined label properties.
If you're trying to add your 2 labels to the superview, with emptyLabel2 on top of emptyLabel, all you need to do is this:
self.view.addSubview(emptyLabel)
self.view.addSubview(emptyLabel2)
They will be added in the order you call addSubview.
If you are asking about how to define the frame so that they show one before the next, you need to modify how you are initializing. Something like this (for example):
let emptyLabel=UILabel(frame: CGRect(x: 0,y: 0, width: self.view.bounds.width, height: 20))
let emptyLabel2=UILabel(frame: CGRect(x: 0,y: 20, width: self.view.bounds.width, height: 20))
self.view.addSubview(emptyLabel)
self.view.addSubview(emptyLabel2)
In reality you may want to find a better way to set the size, or just make 1 label with attributedText so that you do not need 2 labels.

UILabel Position different than expected

Im starting to dabble a bit with swift and ran into a weird behaviour and was wondering if someone could shed some light on why. Im trying to add a UILabel to a UIView and position it in the centre of the UIView. Theoretically this can be achieved with the following.
self.titleContainer = UIView(frame: CGRect(x: self.view.bounds.origin.x, y: self.view.bounds.origin.y
+ 20.0, width: self.view.bounds.width, height: (self.view.bounds.height * cSixth) - 20.0))
self.titleContainer.backgroundColor = UIColor.blackColor()
self.view.addSubview(titleContainer)
self.titleLabel = UILabel()
self.titleLabel.text = "Naughts and Crosses"
self.titleLabel.textColor = UIColor.whiteColor()
self.titleLabel.font = UIFont(name: "Helvetica", size: 10)
self.titleLabel.sizeToFit()
self.titleLabel.center = titleContainer.center
titleContainer.addSubview(self.titleLabel)
however this doesn't seem to take in the top margin. however the next block of code work and positions the label in the center.
self.titleContainer = UIView(frame: CGRect(x: self.view.bounds.origin.x, y: self.view.bounds.origin.y
+ 20.0, width: self.view.bounds.width, height: (self.view.bounds.height * cSixth) - 20.0))
self.titleContainer.backgroundColor = UIColor.blackColor()
self.view.addSubview(titleContainer)
self.titleLabel = UILabel()
self.titleLabel.text = "Naughts and Crosses"
self.titleLabel.textColor = UIColor.whiteColor()
self.titleLabel.font = UIFont(name: "Helvetica", size: 10)
self.titleLabel.sizeToFit()
self.titleLabel.center = CGPointMake(titleContainer.bounds.midX, titleContainer.bounds.midY)
titleContainer.addSubview(self.titleLabel)
Any ideas?
Thanks
Alec
I think you are mixing coordinate systems. self.titleLabel.center is a point inside titleContainer and titleContainer.center is a point inside [titleContainer superview].
The differences are explained in the View Programming Guide -> View and Window Architecture -> View Geometry and Coordinate Systems -> The Relationship of the Frame, Bounds, and Center Properties.

Resources