文字不适合UILabel Swift [英] Text not fitting in UILabel Swift
问题描述
我正在将json数据放入uiwebview中(通过情节提要添加),如果json数据为空,则我将添加一个子视图(以编程方式添加).在该子视图中,我将添加标签.
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)
当我在模拟器上运行它时,我看到"No Description was fou ...",其余内容都在屏幕上消失了.从上面的代码中可以看到,我尝试设置字体大小,字体大小也没有改变,并且尝试将其放在不同的行上
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
推荐答案
解决方案是使标签与descLabel.numberOfLines = 0
多行,并确保其宽度不大于屏幕宽度.
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.
这篇关于文字不适合UILabel Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!