{"id":412,"date":"2025-06-04T11:14:39","date_gmt":"2025-06-05T03:28:38","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=412"},"modified":"2025-06-05T11:14:39","modified_gmt":"2025-06-05T03:28:38","slug":"swift-%e8%87%aa%e9%81%a9%e6%87%89%e5%af%ac%e5%ba%a6-label","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e8%87%aa%e9%81%a9%e6%87%89%e5%af%ac%e5%ba%a6-label\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u6253\u9020\u81ea\u9069\u61c9\u5bec\u5ea6\u7684 UICollectionView Cell"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h2>\u7c21\u8981<\/h2>\n<p>\u5728\u672c\u6559\u5b78\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe UICollectionView \u4e2d\u7684\u81ea\u9069\u61c9\u5bec\u5ea6\u3002\u81ea\u9069\u61c9\u5bec\u5ea6\u5728\u8a31\u591a\u61c9\u7528\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u986f\u793a\u52d5\u614b\u5167\u5bb9\u6642\u3002\u4eca\u5929\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u6839\u64da\u6587\u672c\u9577\u5ea6\u81ea\u52d5\u8abf\u6574 cell \u7684\u5bec\u5ea6\u3002<\/p>\n<h2>\u8a2d\u5b9a UICollectionView<\/h2>\n<p>\u8981\u63a7\u5236 cell \u7684\u5927\u5c0f\uff0c\u6211\u5011\u9996\u5148\u9700\u8981\u8a2d\u7f6e collection view \u7684 layout\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8a2d\u7f6e cell \u7684\u57fa\u672c\u5927\u5c0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">layout.itemSize = CGSize(width: 100, height: 30)<\/code><\/pre>\n<p>\u82e5\u5e0c\u671b\u5c0d\u6bcf\u500b index row \u7684 cell \u5927\u5c0f\u9032\u884c\u66f4\u7d30\u7dfb\u7684\u63a7\u5236\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u4f5c collection view layout \u7684 delegate \u65b9\u6cd5\uff0c\u4f86\u52d5\u614b\u8fd4\u56de\u6bcf\u500b cell \u7684 CGSize\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {\n    var cellSize = CGSize()\n    return cellSize\n}<\/code><\/pre>\n<p>\u8acb\u6ce8\u610f\uff0c\u9019\u9700\u8981\u5728\u985e\u5225\u5ba3\u544a\u4e2d\u9075\u5faa UICollectionViewDelegateFlowLayout \u5354\u8b70\uff0c\u9019\u6a23\u624d\u80fd\u4f7f\u7528\u9019\u4e9b\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout {}<\/code><\/pre>\n<h2>\u6578\u64da\u6e96\u5099<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u6e96\u5099\u4e00\u500b\u5305\u542b\u7be9\u9078\u6a19\u984c\u7684\u6578\u7d44\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">var filterTitleArr: [String] = [\"\u5206\u985e\", \"\u50f9\u683c\u7bc4\u570d\", \"\u5916\u9001\u8cbb\u4e0a\u9650\", \"\u98f2\u98df\u9650\u5236\"]<\/code><\/pre>\n<p>\u6bcf\u500b\u7be9\u9078\u6a19\u984c\u7684\u9577\u5ea6\u4e0d\u540c\uff0c\u56e0\u6b64\u6211\u5011\u9700\u8981\u6e2c\u91cf\u6bcf\u500b\u6587\u5b57\u7684\u5bec\u5ea6\u3002\u4ee5\u4e0b\u662f\u81ea\u5b9a\u7fa9\u7684 textSize \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u64da\u5b57\u9ad4\u548c\u6700\u5927\u5c3a\u5bf8\u4f86\u8a08\u7b97\u6587\u672c\u7684\u5927\u5c0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ MARK: - Custom Methods\nfunc textSize(text: String, font: UIFont, maxSize: CGSize) -> CGSize {\n    return text.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font: font], context: nil).size\n}<\/code><\/pre>\n<h2>\u8a08\u7b97 Cell \u5c3a\u5bf8<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u56de\u5230 collectionViewLayout \u7684\u65b9\u6cd5\u4e2d\uff0c\u5229\u7528\u525b\u525b\u7684 textSize \u65b9\u6cd5\u4f86\u8a08\u7b97\u6bcf\u500b cell \u7684\u5bec\u5ea6\uff0c\u4e26\u8fd4\u56de\u76f8\u61c9\u7684 CGSize\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {\n    let textFont = UIFont.systemFont(ofSize: 17)\n    let textString = filterTitleArr[indexPath.item]\n    let textMaxSize = CGSize(width: 240, height: CGFloat.greatestFiniteMagnitude)\n    let textLabelSize = self.textSize(text: textString, font: textFont, maxSize: textMaxSize)\n\n    \/\/ +40 \u662f\u53f3\u65b9 icon \u5bec\u5ea6 + \u5176\u4ed6\u8a2d\u5b9a\n    let cellSize = CGSize(width: textLabelSize.width + 40, height: 30)\n    return cellSize\n}<\/code><\/pre>\n<p>\u5728 cellForItemAt indexPath \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u5c07\u6587\u672c\u8a2d\u7f6e\u5230 cell \u4e2d\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n    let cell: HomeCell = collectionView.dequeueReusableCell(withReuseIdentifier: \"myCell\", for: indexPath) as! HomeCell\n    cell.textLabel.text = filterTitleArr[indexPath.item]\n    cell.iconImg?.image = UIImage(named: \"down\")\n    return cell\n}<\/code><\/pre>\n<h2>\u52d5\u614b\u986f\u793a\u7be9\u9078\u6a19\u984c<\/h2>\n<p>\u82e5\u8981\u8b93\u7b2c\u4e00\u500b cell \u986f\u793a\u70ba\u5713\u5f62\u4e26\u4fdd\u6301\u6a23\u5f0f\u4e00\u81f4\uff0c\u6211\u5011\u53ef\u4ee5\u5728 layout \u65b9\u6cd5\u4e2d\u9032\u884c\u5224\u65b7\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {\n    let cellSize: CGSize\n    if indexPath.item == 0 {\n        cellSize = CGSize(width: 30, height: 30) \/\/ \u5713\u5f62\n    } else {\n        \/\/ \u7e7c\u7e8c\u4f7f\u7528\u4e4b\u524d\u7684\u8a08\u7b97\u908f\u8f2f\n    }\n    return cellSize\n}<\/code><\/pre>\n<h2>\u6700\u4f73\u5be6\u8e10\uff1a\u907f\u514d\u8a18\u61b6\u9ad4\u554f\u984c<\/h2>\n<p>\u5728\u4f7f\u7528 UIImageView \u6642\uff0c\u6700\u597d\u907f\u514d\u6bcf\u6b21\u91cd\u8907\u6dfb\u52a0\u65b0\u5716\u7247\uff0c\u9019\u6a23\u6703\u5c0e\u81f4\u8a18\u61b6\u9ad4\u4f7f\u7528\u904e\u9ad8\u3002\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 UIStackView \u4f86\u7d44\u7e54\u4f60\u7684\u5143\u4ef6\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ba1\u7406\u986f\u793a\u548c\u96b1\u85cf\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {\n    let cell: HomeCell = collectionView.dequeueReusableCell(withReuseIdentifier: \"myCell\", for: indexPath) as! HomeCell\n\n    if indexPath.item == 0 {\n        \/\/ \u96b1\u85cf\u6587\u5b57\u548c icon\n    } else {\n        \/\/ \u986f\u793a\u6587\u5b57\u548c icon\n    }\n\n    return cell\n}<\/code><\/pre>\n<p><strong>\u7c21\u55ae Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/ZrtpA64.gif\" alt=\"UICollectionView \u81ea\u9069\u61c9\u5bec\u5ea6\u793a\u7bc4\" title=\"\"><\/p>\n<p><strong>\u539f APP<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190923\/201122716YyzRMQEhT.png\" alt=\"\u539f APP \u754c\u9762\" title=\"\"><\/p>\n<blockquote><p>\n  \u5927\u529f\u544a\u6210\uff01\u73fe\u5728\u4f60\u5df2\u7d93\u638c\u63e1\u4e86\u5982\u4f55\u5728 Swift \u4e2d\u5be6\u73fe UICollectionView \u7684\u81ea\u9069\u61c9\u5bec\u5ea6\u3002\n<\/p><\/blockquote>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UICollectionView \u7684\u81ea\u9069\u61c9\u5bec\u5ea6\u6709\u4ec0\u9ebc\u5be6\u969b\u61c9\u7528\uff1f<\/h3>\n<p>A1: \u81ea\u9069\u61c9\u5bec\u5ea6\u53ef\u4ee5\u7528\u65bc\u986f\u793a\u52d5\u614b\u5167\u5bb9\uff0c\u5982\u6a19\u7c64\u3001\u9078\u55ae\u7b49\uff0c\u78ba\u4fdd\u7528\u6236\u754c\u9762\u6574\u6f54\u4e14\u6613\u65bc\u4f7f\u7528\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u907f\u514d UICollectionView \u7684\u6027\u80fd\u554f\u984c\uff1f<\/h3>\n<p>A2: \u78ba\u4fdd\u91cd\u7528 cell \u4e26\u6b63\u78ba\u7ba1\u7406\u5167\u5b58\uff0c\u4f8b\u5982\u4f7f\u7528 UIImageView \u6642\u907f\u514d\u91cd\u8907\u6dfb\u52a0\u5716\u7247\u3002<\/p>\n<h3>Q3: UICollectionViewCell \u7684\u9ad8\u5ea6\u53ef\u4ee5\u52d5\u614b\u8abf\u6574\u55ce\uff1f<\/h3>\n<p>A3: \u662f\u7684\uff0c\u4f7f\u7528 sizeForItemAt \u65b9\u6cd5\u53ef\u4ee5\u6839\u64da\u5167\u5bb9\u52d5\u614b\u8fd4\u56de cell \u7684\u5c3a\u5bf8\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9019\u6b21\u8981\u4f86\u5b8c\u6210\u4e0a\u65b9\u7be9\u9078\u7684\u7d30\u7bc0<br \/>\n\u4e0a\u6b21\u53ea\u6709\u5be6\u4f5c\u7c21\u55ae\u7248\u7684`collectionView`<br \/>\n\u4f46\u8981\u4ed4\u7d30\u89c0\u5bdf\u4ed6\u6bcf\u500b\u5b57\u5176\u5be6\u90fd\u6709`\u81ea\u9069\u61c9\u5bec\u5ea6`<br \/>\n`\u81ea\u9069\u61c9`\u5728\u5f88\u591a\u5730\u65b9\u90fd\u7528\u5f97\u5230<br \/>\n\u4eca\u5929\u5c31\u4f86\u7814\u7a76 `Swfit \u81ea\u9069\u61c9\u5bec\u5ea6`<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6E","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":1183,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/412\/revisions\/1183"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}