gpt4 book ai didi

ios - 如何根据Button的Title将autoSize设置为UIbutton?

转载 作者:可可西里 更新时间:2023-11-01 01:18:21 25 4
gpt4 key购买 nike

我有按钮。为了获得按钮的标题,我向服务器发送请求,然后设置按钮的标题。现在我需要将自动调整大小设置为此按钮。这个怎么做 ?我在按钮的身份检查器的换行符中设置为 CharacterWrap。现在文本显示为多行,但我还需要调整按钮的大小,因为有些文本太大而有些太小。这是它在 Storyboard 中的样子和带有约束的约束,一切正常,我只需要调整按钮的大小。 enter image description here

import UIKit

@IBDesignable
class ButtonTableViewCell: UITableViewCell {

@IBInspectable var selectedColor : UIColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
@IBInspectable var normalColor : UIColor = UIColor.init(red: 59/255, green: 169/255, blue: 246/255, alpha: 1.0)


@IBOutlet weak var variant4button: UIButton!
@IBOutlet weak var variant3button: UIButton!
@IBOutlet weak var variant2Button: UIButton!
@IBOutlet weak var variant1button: UIButton!

这是 4 个按钮。我在这里设置标题:

if (numberOfVariants.count != 0) {
let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
questionTextView.text = "\(Questions[indexPath.row].content!)"
variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside)
variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside)
variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside)
variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside)
let numberOfVars = numberOfVariants[indexPath.row]
if (numberOfVars == 2) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.isHidden = true
variant4.isHidden = true
}
else if (numberOfVars == 3){
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.isHidden = true
}
else if (numberOfVars == 4) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal)
}
}

Jaydeep Vyas enter image description here

最佳答案

只需创建类 uibutton 并将此类分配给所需的按钮

class AutoSizableButton: UIButton
{
override var intrinsicContentSize: CGSize
{
get {
let labelSize = titleLabel?.sizeThatFits(CGSize(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude)) ?? CGSize.zero
let reqiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)

return reqiredButtonSize
}
}
}

也不要忘记在 viewDidload 中添加这个

self.btnDemo.titleLabel?.numberOfLines = 0;
self.btnDemo.titleLabel?.lineBreakMode = .byWordWrapping;

输出 enter image description here

关于ios - 如何根据Button的Title将autoSize设置为UIbutton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45161990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com