字符串不适合swift子视图中的标签 [英] String not fitting in label which is in a subview for swift

查看:119
本文介绍了字符串不适合swift子视图中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将json数据导入uiwebview(通过storyboard添加),如果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.

这篇关于字符串不适合swift子视图中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆