【发布时间】:2016-05-17 09:50:51
【问题描述】:
我正在像这样以编程方式创建按钮:
let stage: Stages = stagesObjectsArray[i]
let button = UIButton(type: .Custom) as UIButton
button.setTitle(stage.name, forState: .Normal)
button.sizeToFit()
button.frame.origin = buttonPosition
let buttonIncrement = button.frame.size.width + padding.width
buttonPosition.x = buttonPosition.x + buttonIncrement
button.backgroundColor = UIColor.blackColor()
button.titleLabel?.font = UIFont(name: "", size: widthOfScreen/3.2)
button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
button.tag = stage.id
button.addTarget(self, action: #selector(GamesAllViewController.buttonPressedTopMenu(_:)), forControlEvents: .TouchUpInside)
buttonView.addSubview(button)
如果按钮的宽度变得太大(例如超过屏幕宽度的一半),我想实现但不知道如何将按钮的标题放在 2 行上。
我发现了一些关于这个主题的问题,但他们都假设他们知道按钮的标题,而在我的例子中它是动态的,我不知道它可能是什么。
我试过这段代码,但没有任何变化:
button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2
有人可以帮忙吗?
【问题讨论】:
标签: ios iphone xcode swift uibutton