Swift - UIButton with two lines of text

前端 未结 10 1676
离开以前
离开以前 2020-11-30 23:38

I was wondering if it is possible to create a UIButton with two lines of text. I need each line to have a different font size. The first line will be 17 point and the seco

10条回答
  •  醉话见心
    2020-12-01 00:32

    One way to do it is with labels, I guess. I did this, and it seems to work ok. I could create this as a UIButton and then expose the labels, I guess. I don't know if this makes any sense.

        let firstLabel = UILabel()
    
        firstLabel.backgroundColor = UIColor.lightGrayColor()
        firstLabel.text = "Hi"
        firstLabel.textColor = UIColor.blueColor()
        firstLabel.textAlignment = NSTextAlignment.Center
        firstLabel.frame = CGRectMake(0, testButton.frame.height * 0.25, testButton.frame.width, testButton.frame.height * 0.2)
        testButton.addSubview(firstLabel)
    
        let secondLabel = UILabel()
    
        secondLabel.backgroundColor = UIColor.lightGrayColor()
        secondLabel.textColor = UIColor.blueColor()
        secondLabel.font = UIFont(name: "Arial", size: 12)
        secondLabel.text = "There"
        secondLabel.textAlignment = NSTextAlignment.Center
        secondLabel.frame = CGRectMake(0, testButton.frame.height * 0.5, testButton.frame.width, testButton.frame.height * 0.2)
        testButton.addSubview(secondLabel)
    

提交回复
热议问题