{"id":271,"date":"2025-06-02T10:20:03","date_gmt":"2025-06-05T03:18:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=271"},"modified":"2025-06-05T10:20:03","modified_gmt":"2025-06-05T03:18:03","slug":"swift-uilabel-%e8%a8%ad%e7%bd%ae%e5%85%a7%e9%82%8a%e8%b7%9d-adding-space-padding-to-a-uilabel","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-uilabel-%e8%a8%ad%e7%bd%ae%e5%85%a7%e9%82%8a%e8%b7%9d-adding-space-padding-to-a-uilabel\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5728 UILabel \u4e2d\u8a2d\u7f6e\u5167\u908a\u8ddd\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>\u5728 Swift \u4e2d\uff0c\u96d6\u7136 UIButton \u63d0\u4f9b\u4e86 `imageEdgeInsets` \u548c `titleEdgeInsets` \u7528\u65bc\u8a2d\u7f6e\u5167\u908a\u8ddd\uff0c\u4f46 UILabel \u4e26\u6c92\u6709\u9019\u6a23\u7684\u5167\u5efa\u5c6c\u6027\u3002\u82e5\u9700\u8981\u5728 UILabel \u4e2d\u8a2d\u7f6e\u5167\u908a\u8ddd\uff0c\u53ef\u4ee5\u900f\u904e\u7e7c\u627f UILabel \u4e26\u81ea\u8a02\u908a\u8ddd\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u91dd\u5c0d Swift 2025 \u7248\u672c\u7684\u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u8b93\u6211\u5011\u4e00\u8d77\u4f86\u770b\u770b\u5982\u4f55\u9032\u884c\u8a2d\u7f6e\u3002<\/p>\n<p>## \u65b0\u589e UILabel \u6a94\u6848<\/p>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u540d\u70ba `UILabelPadding.swift` \u7684 Swift \u6a94\u6848\uff0c\u7528\u65bc\u5b9a\u7fa9\u81ea\u8a02\u7684 UILabel \u985e\u5225\u3002\u5728\u9019\u500b\u985e\u5225\u4e2d\uff0c\u6211\u5011\u6703\u5b9a\u7fa9\u4e00\u500b `UIEdgeInsets` \u8b8a\u6578\u4f86\u8a2d\u7f6e\u4e0a\u4e0b\u7684\u5167\u908a\u8ddd\u3002<\/p>\n<p>### UILabelPadding.swift<\/p>\n<p>&#8220;`swift<br \/>\nimport UIKit<\/p>\n<p>class UILabelPadding: UILabel {<br \/>\n    let padding = UIEdgeInsets(top: 2, left: 0, bottom: 2, right: 0)<\/p>\n<p>    override func drawText(in rect: CGRect) {<br \/>\n        super.drawText(in: rect.inset(by: padding))<br \/>\n    }<\/p>\n<p>    override var intrinsicContentSize: CGSize {<br \/>\n        let superContentSize = super.intrinsicContentSize<br \/>\n        let width = superContentSize.width + padding.left + padding.right<br \/>\n        let height = superContentSize.height + padding.top + padding.bottom<br \/>\n        return CGSize(width: width, height: height)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5275\u5efa\u4e86\u4e00\u500b `UILabelPadding` \u985e\u5225\uff0c\u900f\u904e\u91cd\u5beb `drawText(in:)` \u548c `intrinsicContentSize` \u65b9\u6cd5\uff0c\u4f86\u5be6\u73fe\u4e0a\u4e0b\u7684\u5167\u908a\u8ddd\u8a2d\u7f6e\u3002<\/p>\n<p>## \u5982\u4f55\u4f7f\u7528\u81ea\u8a02 UILabel<\/p>\n<p>\u5728\u60a8\u7684 Storyboard \u6216 Xib \u4e2d\uff0c\u60a8\u53ef\u4ee5\u5c07 UILabel \u7684\u985e\u5225\u8a2d\u7f6e\u70ba `UILabelPadding`\uff0c\u6216\u662f\u76f4\u63a5\u5728\u7a0b\u5f0f\u78bc\u4e2d\u5be6\u4f8b\u5316\u3002<\/p>\n<p>### \u793a\u4f8b\u4ee3\u78bc<\/p>\n<p>&#8220;`swift<br \/>\nlet myLabel = UILabelPadding()<br \/>\nmyLabel.text = &#8220;\u9019\u662f\u4e00\u500b\u5e36\u6709\u5167\u908a\u8ddd\u7684 UILabel&#8221;<br \/>\nmyLabel.backgroundColor = .lightGray<br \/>\nmyLabel.textAlignment = .center<br \/>\nmyLabel.frame = CGRect(x: 20, y: 100, width: 280, height: 50)<br \/>\nview.addSubview(myLabel)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u88e1\u7684\u793a\u4f8b\u4ee3\u78bc\u5275\u5efa\u4e86\u4e00\u500b\u5e36\u6709\u5167\u908a\u8ddd\u7684 UILabel\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u8996\u5716\u4e2d\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<p>&#8211; **\u6587\u5b57\u986f\u793a\u4e0d\u5168**\uff1a\u78ba\u4fdd UILabel \u7684 `frame` \u8a2d\u7f6e\u6b63\u78ba\uff0c\u4e26\u4e14\u5167\u908a\u8ddd\u7684\u503c\u4e0d\u6703\u4f7f\u6587\u5b57\u8d85\u51fa UILabel \u7684\u7bc4\u570d\u3002<br \/>\n&#8211; **\u7121\u6cd5\u986f\u793a\u80cc\u666f\u984f\u8272**\uff1a\u5982\u679c UILabel \u7684 `backgroundColor` \u5c6c\u6027\u672a\u8a2d\u7f6e\uff0c\u53ef\u80fd\u7121\u6cd5\u770b\u5230\u5167\u908a\u8ddd\u6548\u679c\uff0c\u8acb\u78ba\u8a8d\u5df2\u8a2d\u7f6e\u80cc\u666f\u984f\u8272\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u9069\u7528\u65bc UILabel\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5c07\u5176\u64f4\u5c55\u5230\u5176\u4ed6 UIKit \u63a7\u4ef6\u3002\u900f\u904e\u7e7c\u627f\u548c\u81ea\u8a02\uff0c\u60a8\u53ef\u4ee5\u70ba\u4efb\u4f55\u8996\u5716\u6dfb\u52a0\u5167\u908a\u8ddd\u529f\u80fd\u3002<\/p>\n<p>![UILabelPadding \u793a\u4f8b](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/09\/wp_editor_md_69c8f10b1d20d1b34f30dc55f54729c6.jpg)<\/p>\n<p>\u5b8c\u6210\u5f8c\uff0c\u60a8\u7684 UILabel \u5c07\u6703\u6709\u9810\u7559\u7684\u4e0a\u4e0b\u7a7a\u9593\uff0c\u8b93\u6587\u5b57\u5448\u73fe\u66f4\u52a0\u7f8e\u89c0\u3002<\/p>\n<p>![DEMO](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/09\/wp_editor_md_a41c7f3819942031f71b239b514f1d77.jpg)<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54<\/p>\n<p>**Q1: \u5982\u4f55\u5728 UILabel \u4e2d\u8a2d\u7f6e\u4e0d\u540c\u7684\u5167\u908a\u8ddd\uff1f**<br \/>\n\u60a8\u53ea\u9700\u4fee\u6539 `padding` \u8b8a\u6578\u7684\u503c\u5373\u53ef\uff0c\u4f8b\u5982\u8a2d\u7f6e\u70ba `UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)`\u3002<\/p>\n<p>**Q2: UILabelPadding \u53ef\u4ee5\u8207 Auto Layout \u4e00\u8d77\u4f7f\u7528\u55ce\uff1f**<br \/>\n\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u4f7f\u7528 Auto Layout \u7684\u5834\u5408\u4e2d\u4f7f\u7528 UILabelPadding\uff0c\u78ba\u4fdd\u7d04\u675f\u8a2d\u7f6e\u6b63\u78ba\u5373\u53ef\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728 SwiftUI \u4e2d\u5be6\u73fe\u76f8\u540c\u7684\u6548\u679c\uff1f**<br \/>\n\u5728 SwiftUI \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `padding()` \u4fee\u98fe\u7b26\u4f86\u8f15\u9b06\u8a2d\u7f6e\u5167\u908a\u8ddd\uff0c\u4f8b\u5982 `Text(&#8220;Hello&#8221;).padding(.vertical, 10)`\u3002<\/p>\n<p>\u9019\u7bc7\u6587\u7ae0\u5e0c\u671b\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728 Swift \u4e2d\u8a2d\u7f6e UILabel \u7684\u5167\u908a\u8ddd\u3002\u5982\u679c\u60a8\u6709\u5176\u4ed6\u554f\u984c\uff0c\u6b61\u8fce\u96a8\u6642\u63d0\u554f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822cSwift UIButton\u90fd\u6709imageEdgeInsets\u6216\u662ftitleEdgeInsets\u53ef\u4ee5\u8a2d\u7f6e<br \/>\n\u4f46UILabel\u5c31\u6c92\u9019\u7a2e\u5167\u5efa\u51fd\u5f0f\u53ef\u4ee5\u7528<br \/>\n\u9019\u6642\u5019\u5982\u679c\u9700\u8981\u8a2d\u7f6e\u5167\u908a\u8ddd\u7684\u8a71\u53ef\u4ee5\u9019\u6a23\u505a<\/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":[69,70,71],"class_list":["post-271","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","tag-edgeinset","tag-padding","tag-uilabel","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-4n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/271","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=271"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":7553,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/271\/revisions\/7553"}],"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=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}