I'm trying to get my UILabel to get wider (along with its border and background color) as the content gets more - and then less when the content is reduced.
Where do I go to get started, I've looked at the Attributes Inspectors and it looks like this can only be done with code (which I'm fine with).
I thought adding two labels in a horizontal stack would do the trick, but it doesn't update in real-time (it will update the label only on launch).
Try using :
myLabel.sizeToFit()
on your label.This should update the label's frame to fit the content.
let label:UILabel = UILabel()
label.textColor=UIColor.black
label.font = UIFont(name: "Halvetica", size: 17)
label.numberOfLines = 1
label.text = "your string"
label.sizeToFit()
label.frame = CGRect(x: 5, y: imageView.frame.height+10, width: label.frame.width, height:label.frame.height)
Related
The problem I am facing is that UILabel will break line in the middle of the word although I am using word wrapping.
You can create a new project and replace content of view controller to see the result:
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
label.center = CGPoint(x: view.frame.midX, y: view.bounds.midY)
label.numberOfLines = 2 // Setting this to 1 produces expected result
label.lineBreakMode = .byWordWrapping
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
view.addSubview(label)
label.text = "Singlewordtext"
label.backgroundColor = .red
}
This produces 2 lines of text which is broken in the middle of the word. The reason this naturally happens is because the word itself is wider than the label itself so it makes sense (I guess). But I would hope that it would use adjustsFontSizeToFitWidth and minimumScaleFactor prior to breaking it. If I set it to single line (label.numberOfLines = 1) I get expected result which is that the text will shrink instead of break. Note that doing so in this case will fit all of the text inside the label.
The question is, is there a configuration on UILabel to prevent line break in such case? Or is there some other elegant solution?
A current result:
Desired result (produced by using label.numberOfLines = 1):
Do note that I still do need to have 2 lines enabled to nicely display for instance label.text = "Three words fit".
I'm using the OpenSans font in my project but when I adjust its size to a value greater than 1, its size doesn't change. If I set its size with 0, it disappears. I don't understand why it behaves like that. Can anyone explain that to me?
Here is the code:
let RememberMe = UILabel(frame: CGRect(x: checkbox.frame.maxX + view.frame.width*0.05, y: 0, width: ForgetPasswordcontainer.frame.width*0.3, height: ForgetPasswordcontainer.frame.height/2))
RememberMe.text = "Remember Me"
RememberMe.font = UIFont(name: "OpenSans-Regular", size: 1)
RememberMe.textColor = Colors().blue
RememberMe.baselineAdjustment = .alignCenters
RememberMe.adjustsFontSizeToFitWidth = true
RememberMe.textAlignment = .center
ForgetPasswordcontainer.addSubview(RememberMe)
Its not changing because of this line:
RememberMe.adjustsFontSizeToFitWidth = true
No matter what you set the size to, once you get to that line you are once again resizing the font to fit the label that the text is contained in. Either remove that line or change the frame of the label. Also, since it is a custom font you need to be sure that you have registered it in your Info.plist.
Its working for me. Please check :
RememberMe.font = UIFont(name: "Open Sans", size: 10)
And add TTF file to your target.
hey have a look at below code
let RememberMe = UILabel(frame: CGRect(x: 10, y: 50, width: self.view.frame.size.width , height: 100))
RememberMe.text = "Remember Me"
//This add you to selected font and of minimum size which will be adjusted to width itself so no .. appears
RememberMe.font = UIFont(name: "OpenSans-Regular", size: 1000)
RememberMe.textColor = UIColor.blue
RememberMe.baselineAdjustment = .alignCenters
//Need to add this externally if you want to explicitly increase label size
RememberMe.font = UIFont.systemFont(ofSize: 100)
RememberMe.adjustsFontSizeToFitWidth = true
RememberMe.textAlignment = .center
self.view.addSubview(RememberMe)
Output :
1) Must see the frame you provide it and thats correct I think as you are taking this label in other subviews
2) need to provide RememberMe.font = UIFont.systemFont(ofSize: 100) this as to give a size to system Label you are adding as to explicitly increase minimum system font size
Hope it helps
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
}
I have been trying for hours now to find the boundingRect of a string in a UILabel I have, but nothing seems to be working.
From what I understand, boundingRect returns the size of the actual text in the label, not the label's size or something like that. This is true, right?
I have a UILabel called messageLabel which contains some text that wraps to an unlimited number of lines.
The code I have now is this:
let labelRect = (message as NSString).boundingRect(with: messageLabel.frame.size,
options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName : messageLabel.font],
context: nil)
Unfortunately, this returns totally wrong dimensions for my text.
What is the correct way to return the dimensions of text in a multiline UILabel?
Use:
let sizeToFit = CGSize(width: messageLabel.frame.size.width,
height: CGFloat.greatestFiniteMagnitude)
let textSize = messageLabel.sizeThatFits(sizeToFit)
Anyway, the way you did it should work as well (you can see on playground both functions return same size):
I've added a sample view to the playground, so you can see, the label has black border, and the text fits inside, and is smaller than label. Size is computer properly with both sizeToFit and boundingRect methods (but boundingRect returns not rounded values). I've use this computed size to create a green background view under the text, and it fits it properly.
I think you need to Try this
let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: _screenSize.width - 30, height: 5))
messageLabel.font = self.txtDescription.font
messageLabel.numberOfLines = 0
messageLabel.text = "Your Massage"
messageLabel.numberOfLines = 0
messageLabel.sizeToFit()
print(messageLabel.frame.size.height)
Remove all code Just try this Hope it will wirk
I'm trying to make a label multilines, that fits the screen, so i'm using the CGFloat.max to make it's height dynamic... but using CGFloat.max is causing the label to ignore the positioning, any always keep on the position 0 in the Y axis....
Even passing any variable ou even a number to it, keeps on the 0 in Y axis
Any ideas to fix??
let label: UILabel = UILabel(frame: CGRectMake(10, 50, screenWidthArea, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.text = text
label.sizeToFit()
self.view.addSubview(label)
Actually I've changed CGFloat.max to 0 and it worked well... Still no idea for the reason CGFloat.max locks the label on the top of screen.
let label: UILabel = UILabel(frame: CGRectMake(10, 50, screenWidthArea, 0))