Swift UIlabels在uistackview中没有正确显示

时间:2017-04-19 14:41:04

标签: ios swift swift3 uilabel

我试图以编程方式将uilabels添加到stackview中,但由于某种原因它没有正确显示。这就是我所拥有的:

let label = UILabel()
label.textColor = UIColor.black
label.text = "Text that goes before "

let matlabel = MTMathUILabel()
matlabel.labelMode = MTMathUILabelMode.text
matlabel.latex = "$\\frac{x}{y}$"
matlabel.textColor = UIColor.black

let endLabel = UILabel()
label.textColor = UIColor.black
label.text = "? after"

self.textStack.addArrangedSubview(label)
self.textStack.addArrangedSubview(matlabel)      
self.textStack.addArrangedSubview(endLabel) 

这就是我得到的

2 个答案:

答案 0 :(得分:1)

你创建了一个" endLabel"但你没有在上面设置文字。你错误地在"标签"上设置了文字。所以它只是覆盖你的"标签"文本。尝试以下代码:

let label = UILabel()
label.textColor = UIColor.black
label.text = "Text that goes before "

let matlabel = MTMathUILabel()
matlabel.labelMode = MTMathUILabelMode.text
matlabel.latex = "$\\frac{x}{y}$"
matlabel.textColor = UIColor.black

let endLabel = UILabel()
endLabel.textColor = UIColor.black
endLabel.text = "? after"

self.textStack.addArrangedSubview(label)
self.textStack.addArrangedSubview(matlabel)      
self.textStack.addArrangedSubview(endLabel) 

答案 1 :(得分:0)

嗯,我认为你真正想做的是:

self.textStack.addArrangedSubview(label)
self.textStack.addArrangedSubview(matlabel)   
self.textStack.addArrangedSubview(endLabel)

但是,就你的观点而言,"标签"没有出现。您是否尝试过设置对齐和分配?