gpt4 book ai didi

ios - 根据 UITableView Swift 中的文本大小扩展标签/单元格大小

转载 作者:IT王子 更新时间:2023-10-29 05:46:46 24 4
gpt4 key购买 nike

我是 iOS 开发新手。我的细胞大小有问题。我没有使用 Auto-Layout 功能。我当前的 TableView 单元格看起来像 like this .我想制作在 image 中选择的标签尺寸根据该标签的文本内容动态扩展。

提前致谢。

最佳答案

我认为像下面这样的 swift 版本,它计算更接近的值而不是确切的高度,例如

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

var messageArray:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
messageArray = ["One of the most interesting features of Newsstand is that once an asset downloading has started it will continue even if the application is suspended (that is: not running but still in memory) or it is terminated. Of course during while your app is suspended it will not receive any status update but it will be woken up in the background",
"In case that app has been terminated while downloading was in progress, the situation is different. Infact in the event of a finished downloading the app can not be simply woken up and the connection delegate finish download method called, as when an app is terminated its App delegate object doesn’t exist anymore. In such case the system will relaunch the app in the background.",
" If defined, this key will contain the array of all asset identifiers that caused the launch. From my tests it doesn’t seem this check is really required if you reconnect the pending downloading as explained in the next paragraph.",
"Whale&W",
"Rcokey",
"Punch",
"See & Dive"]
}

在上面我们有一个包含不同长度字符串的数组

 func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return messageArray.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell;
if(cell == nil)
{
cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "CELL")
cell?.selectionStyle = UITableViewCellSelectionStyle.none
}
cell?.textLabel.font = UIFont.systemFontOfSize(15.0)
cell?.textLabel.sizeToFit()
cell?.textLabel.text = messageArray[indexPath.row]
cell?.textLabel.numberOfLines = 0
return cell!;
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
var height:CGFloat = self.calculateHeightForString(messageArray[indexPath.row])
return height + 70.0
}

func calculateHeight(inString:String) -> CGFloat
{
let messageString = inString
let attributes : [String : Any] = [NSFontAttributeName : UIFont.systemFont(ofSize: 15.0)]

let attributedString : NSAttributedString = NSAttributedString(string: messageString, attributes: attributes)

let rect : CGRect = attributedString.boundingRect(with: CGSize(width: 222.0, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil)

let requredSize:CGRect = rect
return requredSize.height
}

在一个新项目中尝试它,只需添加 tableview 并设置其数据源和委托(delegate),然后通过上面的代码并运行

结果如下所示

enter image description here

关于ios - 根据 UITableView Swift 中的文本大小扩展标签/单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114483/

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