自動(dòng)計(jì)算Text高度:
let textLabel = ASTextNode()
textLabel.attributedText = NSAttributedString(string: "自動(dòng)計(jì)算寬高的文字", attributes: [NSForegroundColorAttributeName:UIColor.cyan,
NSFontAttributeName:UIFont.systemFont(ofSize: 18)])
textLabel.backgroundColor = UIColor.darkGray
//text的最小占用寬高
let CGsizeZero = CGSize(width: 0, height: 0)
//text的最大占用寬高
let CGsizeMax = CGSize(width: view.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let asRange = ASSizeRange(min: CGsizeZero, max: CGsizeMax)
textLabel.layoutThatFits(asRange)
//獲得text的總高度
print(textLabel.calculatedSize.height)
textLabel.frame = CGRect(x: 0, y: 0, width: textLabel.calculatedSize.width, height: textLabel.calculatedSize.height)
view.addSubview(textLabel.view)
ASTableNode:
class ViewController: UIViewController,ASTableDelegate,ASTableDataSource{
let asTableNode = ASTableNode()
let mockData = [
[NSURL(string: "http://tp1.sinaimg.cn/3985473000/180/5742244430/0")!, "楊大大117", "不論我們最后生疏到什么樣子 要記住 曾經(jīng)對(duì)你的好都是真的 ?"],
[NSURL(string: "http://tp3.sinaimg.cn/2466802634/180/5740492182/0")!, "孟礬礬", "溫和而堅(jiān)定。"],
[NSURL(string: "http://tp2.sinaimg.cn/1736940353/180/5634177627/0")!, "郭德欣", "廣州交通電臺(tái)FM106.1主持人"],
[NSURL(string: "http://tp4.sinaimg.cn/2379086631/180/40052837834/0")!, "我是你景兒", "店鋪已更名為:JiLovèng 。大家可以在淘寶寶貝直接搜索這個(gè),不分大小寫。[心]jiloveng"]
]
override func viewDidLoad() {
super.viewDidLoad()
asTableNode.delegate = self
asTableNode.dataSource = self
view.addSubview(asTableNode.view)
func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int {
return mockData.count
}
func tableNode(_ tableNode: ASTableNode, nodeForRowAt indexPath: IndexPath) -> ASCellNode {
let cellNode = TestCellNode()
if indexPath.row < mockData.count {
let item = mockData[indexPath.row]
if let URL = item[0] as? NSURL,
let title = item[1] as? String,
let subTitle = item[2] as? String {
cellNode.configureData(iconURL: URL, title: title, subTitle: subTitle)
}
}
return cellNode
}
}
ASCellNode:
class TestCellNode: ASCellNode {
let iconImageNode = ASImageNode()
let titleLabel = ASTextNode()
override init() {
super.init()
addSubnode(iconImageNode)
addSubnode(titleLabel)
}
override func layout() {
iconImageNode.frame = CGRect(x: 10, y: 10, width: 50, height: 50)
titleLabel.frame = CGRect(x: 70, y: 10, width: titleLabel.calculatedSize.width, height: titleLabel.calculatedSize.height)
}
override func calculateSizeThatFits(_ constrainedSize: CGSize) -> CGSize {
titleLabel.backgroundColor = UIColor.darkGray
//text的最小占用寬高
let CGsizeZero = CGSize(width: 0, height: 0)
//text的最大占用寬高
let CGsizeMax = CGSize(width: constrainedSize.width - 80, height: CGFloat.greatestFiniteMagnitude)
let asRange = ASSizeRange(min: CGsizeZero, max: CGsizeMax)
titleLabel.layoutThatFits(asRange)
if titleLabel.calculatedSize.height < 50 {
return CGSize(width: constrainedSize.width, height: 70)
}else {
return CGSize(width: constrainedSize.width,
height: 10 + titleLabel.calculatedSize.height + 10)
}
}
func configureData(iconURL: NSURL, title: String, subTitle: String) {
// iconURL:圖像接口,暫時(shí)用背景色替代
iconImageNode.backgroundColor = UIColor.cyan
titleLabel.attributedText = NSAttributedString(string: title, attributes: [
NSForegroundColorAttributeName: UIColor.black,
NSFontAttributeName: UIFont.systemFont(ofSize: 18)
])
// subTitleLabel:
}
}
點(diǎn)擊Cell展開
func tableNode(_ tableNode: ASTableNode, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableNode.nodeForRow(at: indexPath) as? TestCellNode else {
return
}
//取消點(diǎn)擊效果
tableNode.deselectRow(at: indexPath, animated: true)
UIView.animate(withDuration: 0.3) {
self.asTableNode.performBatchUpdates({
let cellHeight = cell.frame.height
if cell.isExpanded {
//如果是展開狀態(tài),點(diǎn)擊縮回
cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight - 50)
//如果是正常狀態(tài),點(diǎn)擊展開
} else {
cell.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: cellHeight + 50)
}
cell.isExpanded = !cell.isExpanded
// 滑動(dòng)到點(diǎn)擊的cell
// self.asTableNode.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
}, completion: { (success) in
print(success)
})
}
}