Ios 如何在Swift中更改UILabel的文本属性?
我已经以编程方式设置了UILabel,并试图通过稍后在ViewController中调用的函数更改文本属性,但是当该函数被称为questionLabel时,text保持默认值“Welcome” 基本上,我想实现的是:Ios 如何在Swift中更改UILabel的文本属性?,ios,swift,xcode,uilabel,viewcontroller,Ios,Swift,Xcode,Uilabel,Viewcontroller,我已经以编程方式设置了UILabel,并试图通过稍后在ViewController中调用的函数更改文本属性,但是当该函数被称为questionLabel时,text保持默认值“Welcome” 基本上,我想实现的是: func changeLabelText() { questionLabel.text = "New label text" print(questionLabel.text!) } changeL
func changeLabelText() {
questionLabel.text = "New label text"
print(questionLabel.text!)
}
changeLabelText()
// prints "New label text"
然而,我实际得到的是:
func changeLabelText() {
questionLabel.text = "New label text"
print(questionLabel.text!)
}
changeLabelText()
// prints "Welcome"
我的标签是这样设置的:
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
@IBOutlet var cameraView: UIView!
var questionLabel: UILabel {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.backgroundColor = .white
label.textColor = .black
label.text = "Welcome"
label.textAlignment = .center
label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)
return label
}
有什么建议吗?非常感谢 电流
var questionLabel: UILabel {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.backgroundColor = .white
label.textColor = .black
label.text = "Welcome"
label.textAlignment = .center
label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)
return label
}
是一个计算属性,因此每个访问都会获得一个新的单独实例
questionLabel.text = "New label text" // instance 1
print(questionLabel.text!) // instance 2
相反,你需要一个结束
var questionLabel: UILabel = {
let label = UILabel()
label.lineBreakMode = .byWordWrapping
label.backgroundColor = .white
label.textColor = .black
label.text = "Welcome"
label.textAlignment = .center
label.frame = CGRect(x: 65, y: 100, width: 300, height: 65)
return label
}()
克拉蒙特,
你可以试试这个
假设您想更改标签的某些文本,您总是为此创建两个标签,但更改标签文本颜色的方法是错误的。您可以使用NSMutableAttributeString更改标签的某些文本颜色。首先,您必须找到文本的范围,要更改该文本的颜色,然后将文本范围设置为NSMutableAttributedString对象(与完整字符串相比),然后将标签attributedText设置为NSMutableAttributedString对象。示例:
let strNumber: NSString = "Hello Test" as NSString // you must set your
let range = (strNumber).range(of: "Test")
let attribute = NSMutableAttributedString.init(string: strNumber)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
yourLabel.attributedText = attribute
如果您想在应用程序中多次使用此功能,只需创建UILabel的扩展,它将使操作更加简单:-
extension UILabel {
func halfTextColorChange (fullText : String , changeText : String ) {
let strNumber: NSString = fullText as NSString
let range = (strNumber).range(of: changeText)
let attribute = NSMutableAttributedString.init(string: fullText)
attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)
self.attributedText = attribute
}
}
使用您的标签:-yourLabel=“你好测试”
yourLabel.halfTextColorChange(全文:totalabel.text!,changeText:“Test”)将计算变量更改为惰性初始值设定项,如下所示:
lazy var questionLabel:UILabel={
let label=UILabel()
label.lineBreakMode=.byWordWrapping
label.backgroundColor=.white
label.textColor=.black
label.text=“欢迎”
label.textAlignment=.center
label.frame=CGRect(x:65,y:100,宽度:300,高度:65)
退货标签
}()