Search code examples
iosswiftios8subview

Text not fitting in UILabel Swift


i am getting json data into uiwebview(which is added through storyboard) and if the json data is empty i am adding a subview(which is added programatically).and in that subview i am adding a label.

when i run it i can see the subview and the label but its size is big, so it goes out of the subview. The code i have is

var detailView = UIView(frame: CGRectMake(0, 143, 568, 376)) //Overlapping the UIWebview
//detailView.backgroundColor = UIColor.blackColor()
detailView.contentMode = UIViewContentMode.ScaleAspectFit
detailView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
self.view.addSubview(detailView)
var descLabel = UILabel(frame: CGRectMake(0, 0, 200, 200))

descLabel.textColor = UIColor.redColor()
descLabel.font = UIFont(name:"HelveticaNeue;", size: 6.0)
descLabel.text = "No Description was found" +
                        " for \(self.strFullName)" +
                        " please try again later"
descLabel.textAlignment = NSTextAlignment.Center
descLabel.contentMode = UIViewContentMode.ScaleAspectFit
descLabel.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
detailView.addSubview(descLabel)

When i run it on the simulator i see "No Description was fou..." and the rest of it is gone of the screen. as you can see from above code i have tried to set the font size, the font size does not change either, and also try to put it on different lines

What am i doing wrong which is causing the label to display like this? And how can i fix it.

Thank you in advance for any help provided


Solution

  • The solution is to make the label multi-line with descLabel.numberOfLines = 0, and make sure that its width is not larger than screen width.