I'm working on chat. So I stopped where I should do self-sizing labels to display text.
I tried to make my label as:
messageLabel.frame = CGRectMake(0, 0, self.chatScrollView.frame.size.width - 90, CGFloat.max)
messageLabel.sizeToFit()
messageLabel.frame.origin.x = (self.chatScrollView.frame.size.width - self.messageX) - messageLabel.frame.size.width
messageLabel.frame.origin.y = self.messageY
but my label didn't appear at all. If I make some exact height of my label for example 100 and comment line
//messageLabel.sizeToFit()
so the whole label code:
let messageLabel: UILabel = UILabel()
messageLabel.text = self.messageArray[i]
messageLabel.frame = CGRectMake(0, 0, self.chatScrollView.frame.size.width - 90, CGFloat.max)
messageLabel.backgroundColor = UIColor.groupTableViewBackgroundColor()
messageLabel.numberOfLines = 0
messageLabel.lineBreakMode = .ByWordWrapping
messageLabel.sizeToFit()
messageLabel.textAlignment = .Left
messageLabel.font = UIFont(name: "Apple SD Gothic Neo", size: 20)
messageLabel.textColor = UIColor.grayColor()
messageLabel.layer.zPosition = 20
messageLabel.frame.origin.x = (self.chatScrollView.frame.size.width - self.messageX) - messageLabel.frame.size.width
messageLabel.frame.origin.y = self.messageY
print(self.messageArray[i])
print("label origin \(messageLabel.frame.origin.x, messageLabel.frame.origin.y)")
self.chatScrollView.addSubview(messageLabel)
self.messageY += messageLabel.frame.size.height + 30
let width = self.view.frame.size.width
self.chatScrollView.contentSize = CGSizeMake(width, self.messageY)
self.chatScrollView.bringSubviewToFront(messageLabel)
Edit #1
Label definitely has text (print statement proofs it)
Label origin is on the screen area (second print statement proofs it)
Question: How I can fix it (or how I can make my label appear and be self-sizing label)?
I found the solution!
I just put my
messageLabel.text = self.messageArray[i]
right under the
let messageLabel: UILabel = UILabel()
so my label appeared!
Related
I am trying to increase the size of highlighted colour of a particular date in FSCalendar.The below is the code am using also am attaching.Please let me know what i am missing.Thank you
func setUpCalendar() {
calendar = FSCalendar(frame: CGRect(x: 20, y: 150, width: self.view.frame.size.width - 30, height: 150))
calendar.scrollDirection = .horizontal
calendar.scope = .week
calendar.allowsMultipleSelection = false
calendar.appearance.separators = .interRows
calendar.rowHeight = 30.0
calendar.appearance.headerTitleColor = UIColor(hex: "FF7271")
calendar.appearance.todayColor = UIColor(hex: "FF7271")
calendar.appearance.selectionColor = UIColor(hex: "FFEFEF")
calendar.appearance.titleSelectionColor = .black
calendar.appearance.weekdayTextColor = .systemGray
calendar.appearance.headerMinimumDissolvedAlpha = 0
calendar.appearance.headerTitleFont = UIFont(name: "System", size: 0)
}
hide like this. go to storyboard / xib what you used.
goto design -> go to Atrributed inspector and set header height = 0
as per you define it programatically than just write this line
calendar.headerHeight = 0
I would like to create a UILabel programmatically after specifying the number of lines. I am using Swift. I have the following code:
let newLabel = UILabel()
newLabel.text = "Hello There"
newLabel.font = UIFont.systemFont(ofSize: 16)
newLabel.numberOfLines = 2
newLabel.lineBreakMode = .byWordWrapping
newLabel.sizeToFit()
newLabel.frame.origin.x = 100
newLabel.frame.origin.y = 500
view.addSubview(newLabel)
The problem is that the label has the text on one line, rather than two lines. I have to use sizeToFit because the fontSize is actually dynamic (it is not always 16). How can I make sure that the label is 2 lines? Thanks.
You are not specifying exact frame of your UILabel.so your view just get any width it wants.you can use this for getting result:
newLabel.frame = CGRect(x: 100, y: 500, width: newLabel.frame.width - 1, height: newLabel.frame.height * 2)
But that's not very good and I suggest using AutoLayout
There are many things you can do to break line.
1) You can use "\n" in between the words. But not sure about your usecase and whether this make sense.
2) Thought not recommended you can define the width of the label.
3) Use numberOfLines = 0, if you can go to more than 2 lines. However, if you only want 2 lines then give numberOfLines = 2.
Also use Constraints something like below and not frame:
private let newLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Hello There"
label.font = UIFont.systemFont(ofSize: 16)
label.numberOfLines = 0
return label
}()
view.addSubview(newLabel)
NSLayoutConstraint.activate([
newLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 500),
newLabel.leadingAnchor.constraint(equalTo:view.leadingAnchor, constant: 100),
newLabel.widthAnchor.constraint(equalToConstant: 70)
])
Here is the code I have for UILabel
descriptionText.layer.cornerRadius = 8
descriptionText.layer.borderColor = UIColor.lightGray.cgColor
descriptionText.layer.borderWidth = 2
descriptionText.text = ""
descriptionText.layer.shadowOffset = CGSize(width: -10, height: -10)
descriptionText.layer.shadowRadius = -5.0
descriptionText.backgroundColor = .white
descriptionText.textColor = .black
descriptionText.numberOfLines = 0
descriptionText.lineBreakMode = .byWordWrapping
However This is what it looks like
Why is the Corner Not Transparent or how do you make what is outside the border transparent
Thanks in advance
You need to hide everything outside of the cornerRadius by setting .clipsToBounds on the label to true. Problem is that if you do this you will lose the shadow, as this is outside the bounds as well. Try this:
Place your label inside a container view, constrain the label to the container edges, and create an outlet to it. Then try the following code:
containerView.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderColor = UIColor.lightGray.cgColor
containerView.layer.borderWidth = 2
containerView.layer.shadowOffset = CGSize(width: -10, height: -10)
containerView.layer.shadowRadius = -5.0
containerView.backgroundColor = UIColor.clear
descriptionText.text = "Testing"
descriptionText.cornerRadius = 8
descriptionText.clipsToBounds = true // or descriptionText.layer.masksToBounds = true
descriptionText.backgroundColor = .white
descriptionText.textColor = .black
descriptionText.numberOfLines = 0
descriptionText.lineBreakMode = .byWordWrapping
I’m trying to add a label to an SCNNode. So far I’ve managed to set a SKLabelNode as the material for the SCNNode. It sort of works, I can the SCNNode becomes the background colour of the SKLabelNode but I can’t see the text. Sometime I can see a red haze (the text colour is red) but no readable text.
I also tried setting the material as a UIView and adding a UiLabel as a sub view. Again it sets as I can the whole SCNNode becomes the background colour of the UiLabel but I can’t see any text.
var block = SCNNode()
var spriteScene = SKScene()
var Lbl = SKLabelNode()
lbl.text = “Hello World”
lbl.color = blue (playground colour literal)
lbl. = 2 //I tried various numbers
lbl.fontColor = SKColor.red
spriteScene.addChild(lbl)
I got it after some hit and trial. I had to try different values before I got these size, scale and rotation to display the label as I want.
note: My node here is SCNPlane with 0.3 width and 0.2 height, so size of SKScene and rectangle and position of label are hard coded accordingly.
func addLabel(text: String){
let sk = SKScene(size: CGSize(width: 3000, height: 2000))
sk.backgroundColor = UIColor.clear
let rectangle = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 3000, height: 2000), cornerRadius: 10)
rectangle.fillColor = UIColor.black
rectangle.strokeColor = UIColor.white
rectangle.lineWidth = 5
rectangle.alpha = 0.5
let lbl = SKLabelNode(text: text)
lbl.fontSize = 160
lbl.numberOfLines = 0
lbl.fontColor = UIColor.white
lbl.fontName = "Helvetica-Bold"
lbl.position = CGPoint(x:1500,y:1000)
lbl.preferredMaxLayoutWidth = 2900
lbl.horizontalAlignmentMode = .center
lbl.verticalAlignmentMode = .center
lbl.zRotation = .pi
sk.addChild(rectangle)
sk.addChild(lbl)
let material = SCNMaterial()
material.isDoubleSided = true
material.diffuse.contents = sk
node.geometry?.materials = [material]
node.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(Float(1), Float(1), 1)
node.geometry?.firstMaterial?.diffuse.wrapS = .repeat
node.geometry?.firstMaterial?.diffuse.wrapS = .repeat
}
I am using a font called: Luckiest Guy in a Swift project (Link to the font).
However, in UILabels, it's not vertically aligned to the center even after setting baselineAlignment to the center...
Could you check my code and help me out. Thanks a lot!!
I attached a screen shot here to show the problem (https://www.dropbox.com/s/1hxq68dewtzzkxi/IMG_9733.png?dl=0)
var dynamicLabel: UILabel = UILabel()
dynamicLabel.frame = CGRectMake(0, 0, 60, 35) //Frame (the coordinates are changed here to simply the code)
// *** Color and offset *** //
dynamicLabel.backgroundColor = UIColor.blueColor()
dynamicLabel.textColor = UIColor.whiteColor()
dynamicLabel.shadowColor = UIColor.blackColor()
dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)
// *** Text alignment *** //
dynamicLabel.textAlignment = NSTextAlignment.Center
dynamicLabel.font = UIFont(name: "LuckiestGuy-Regular", size: 25)
dynamicLabel.adjustsFontSizeToFitWidth = true
dynamicLabel.minimumScaleFactor = 0.5
dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// *** Label corner and border *** //
dynamicLabel.layer.masksToBounds = true
dynamicLabel.layer.cornerRadius = 15.0
dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
dynamicLabel.layer.borderWidth = 1.0
// Text
dynamicLabel.text = "Excellent!"
As your explanation to what is happening instead is rather vague I assume that the Label isn't really visible, because with the implementation of dynamicLabel.textAlignment = NSTextAlignment.Center the text should be aligned
I've tried your code in a sample project and the only two things I noticed that were off were the x and y coordinates of the label (as they're set to zero, so the label doesn't really get displayed anyways) and that you didn't add the label to the view:
let dynamicLabel = UILabel(frame: CGRectMake(100, 200, 200, 100))
// *** Color and offset *** //
dynamicLabel.backgroundColor = UIColor.blueColor()
dynamicLabel.textColor = UIColor.whiteColor()
dynamicLabel.shadowColor = UIColor.blackColor()
dynamicLabel.shadowOffset = CGSizeMake(0, 1.0)
dynamicLabel.text = "Excellent!"
// *** Text alignment *** //
dynamicLabel.textAlignment = NSTextAlignment.Center
dynamicLabel.adjustsFontSizeToFitWidth = true
dynamicLabel.minimumScaleFactor = 0.5
dynamicLabel.baselineAdjustment = UIBaselineAdjustment.AlignCenters
// *** Label corner and border *** //
dynamicLabel.layer.masksToBounds = true
dynamicLabel.layer.cornerRadius = 15.0
dynamicLabel.layer.borderColor = UIColor.whiteColor().CGColor
dynamicLabel.layer.borderWidth = 1.0
view.addSubview(dynamicLabel)