{"id":70,"date":"2016-12-24T21:20:00","date_gmt":"2016-12-24T21:20:00","guid":{"rendered":"https:\/\/seimith.io\/?p=70"},"modified":"2024-07-20T00:35:21","modified_gmt":"2024-07-20T00:35:21","slug":"swift-make-uilabel-conform-to-length-of-text","status":"publish","type":"post","link":"https:\/\/seimith.io\/pt\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/","title":{"rendered":"(Swift) Make UILabel Conform to Length of Text"},"content":{"rendered":"<p>Here\u2019s a snippet of how you can make your UILabel conform to the width of your text.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/determined-mcnulty-44b4a3.netlify.app\/static\/0c134e2ae162ca58634ac952e13bad47\/f058b\/img2.png\" alt=\"&quot;Simulator of UILabel conforming to width of text&quot;\" style=\"width:338px;height:auto\" title=\"Simulator of UILabel conforming to width of text\"\/><\/figure>\n\n\n\n<p><a href=\"https:\/\/determined-mcnulty-44b4a3.netlify.app\/static\/0c134e2ae162ca58634ac952e13bad47\/9937c\/img2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<p>You want to use&nbsp;<code>.sizeToFit()<\/code>&nbsp;on your UILabel, otherwise you\u2019d have to hardcode your label\u2019s width in the&nbsp;<code>CGRect<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import UIKit\n\nclass ViewController: UIViewController {\n  override func viewDidLoad() {\n    super.viewDidLoad()\n\n    let myCoolUILabel = UILabel()\n    myCoolUILabel.text = \"My Cool Label\"\n    myCoolUILabel.backgroundColor = UIColor.orange\n    myCoolUILabel.frame = CGRect(x: 100, y: 100, width: 30, height: 30)\n    myCoolUILabel.sizeToFit()\n    self.view.addSubview(myCoolUILabel)\n  }\n}<\/code><\/pre>\n\n\n\n<p>So, no more of this:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/determined-mcnulty-44b4a3.netlify.app\/static\/4e4528c794a3347e7d63b8b012adcb18\/f058b\/img1.png\" alt=\"&quot;Simulator of UILabel not conforming to width of text&quot;\" style=\"width:338px;height:auto\" title=\"Simulator of UILabel not conforming to width of text\"\/><\/figure>\n\n\n\n<p><a href=\"https:\/\/determined-mcnulty-44b4a3.netlify.app\/static\/4e4528c794a3347e7d63b8b012adcb18\/9937c\/img1.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a snippet of how you can make your UILabel conform to the width of your text. You want to use&nbsp;.sizeToFit()&nbsp;on your UILabel, otherwise you\u2019d have to hardcode your label\u2019s width in the&nbsp;CGRect. So, no more of this:<\/p>","protected":false},"author":1,"featured_media":288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20],"class_list":["post-70","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-post","tag-code"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/seimith.io\/pt\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io\" \/>\n<meta property=\"og:description\" content=\"Here\u2019s a snippet of how you can make your UILabel conform to the width of your text. You want to use&nbsp;.sizeToFit()&nbsp;on your UILabel, otherwise you\u2019d have to hardcode your label\u2019s width in the&nbsp;CGRect. So, no more of this:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/seimith.io\/pt\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/\" \/>\n<meta property=\"og:site_name\" content=\"S(ei)mith.io\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-24T21:20:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-20T00:35:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Smith\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smith\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/\"},\"author\":{\"name\":\"Smith\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\"},\"headline\":\"(Swift) Make UILabel Conform to Length of Text\",\"datePublished\":\"2016-12-24T21:20:00+00:00\",\"dateModified\":\"2024-07-20T00:35:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/\"},\"wordCount\":53,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"keywords\":[\"Code\"],\"articleSection\":[\"Post\"],\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/\",\"url\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/\",\"name\":\"(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"datePublished\":\"2016-12-24T21:20:00+00:00\",\"dateModified\":\"2024-07-20T00:35:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#primaryimage\",\"url\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"contentUrl\":\"https:\\\/\\\/seimith.io\\\/wp-content\\\/uploads\\\/2020\\\/12\\\/placeholder_image.webp\",\"width\":700,\"height\":700},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/seimith.io\\\/2016\\\/12\\\/24\\\/swift-make-uilabel-conform-to-length-of-text\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/seimith.io\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Swift) Make UILabel Conform to Length of Text\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#website\",\"url\":\"https:\\\/\\\/seimith.io\\\/\",\"name\":\"s(ei)mith.io\",\"description\":\"Ramblings written by\u00a0Smith Suth\u00a0who works at the intersection of Engineering, Product, and Design.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/seimith.io\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/seimith.io\\\/#\\\/schema\\\/person\\\/315065130ef0017986daf9a1127ce80a\",\"name\":\"Smith\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g\",\"caption\":\"Smith\"},\"sameAs\":[\"https:\\\/\\\/seimith.io\"],\"url\":\"https:\\\/\\\/seimith.io\\\/pt\\\/author\\\/seimithsuth\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/seimith.io\/pt\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/","og_locale":"pt_BR","og_type":"article","og_title":"(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io","og_description":"Here\u2019s a snippet of how you can make your UILabel conform to the width of your text. You want to use&nbsp;.sizeToFit()&nbsp;on your UILabel, otherwise you\u2019d have to hardcode your label\u2019s width in the&nbsp;CGRect. So, no more of this:","og_url":"https:\/\/seimith.io\/pt\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/","og_site_name":"S(ei)mith.io","article_published_time":"2016-12-24T21:20:00+00:00","article_modified_time":"2024-07-20T00:35:21+00:00","og_image":[{"width":700,"height":700,"url":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","type":"image\/webp"}],"author":"Smith","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smith","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#article","isPartOf":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/"},"author":{"name":"Smith","@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a"},"headline":"(Swift) Make UILabel Conform to Length of Text","datePublished":"2016-12-24T21:20:00+00:00","dateModified":"2024-07-20T00:35:21+00:00","mainEntityOfPage":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/"},"wordCount":53,"commentCount":0,"image":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#primaryimage"},"thumbnailUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","keywords":["Code"],"articleSection":["Post"],"inLanguage":"pt-BR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/","url":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/","name":"(Swift) Make UILabel Conform to Length of Text - S(ei)mith.io","isPartOf":{"@id":"https:\/\/seimith.io\/#website"},"primaryImageOfPage":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#primaryimage"},"image":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#primaryimage"},"thumbnailUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","datePublished":"2016-12-24T21:20:00+00:00","dateModified":"2024-07-20T00:35:21+00:00","author":{"@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a"},"breadcrumb":{"@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#primaryimage","url":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","contentUrl":"https:\/\/seimith.io\/wp-content\/uploads\/2020\/12\/placeholder_image.webp","width":700,"height":700},{"@type":"BreadcrumbList","@id":"https:\/\/seimith.io\/2016\/12\/24\/swift-make-uilabel-conform-to-length-of-text\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/seimith.io\/"},{"@type":"ListItem","position":2,"name":"(Swift) Make UILabel Conform to Length of Text"}]},{"@type":"WebSite","@id":"https:\/\/seimith.io\/#website","url":"https:\/\/seimith.io\/","name":"s(ei)mith.io","description":"Ramblings written by\u00a0Smith Suth\u00a0who works at the intersection of Engineering, Product, and Design.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/seimith.io\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Person","@id":"https:\/\/seimith.io\/#\/schema\/person\/315065130ef0017986daf9a1127ce80a","name":"Smith","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a403a51628e8e4553b69960e9eb8b67184121e56ddf2fdf28c5f9515bb518208?s=96&d=mm&r=g","caption":"Smith"},"sameAs":["https:\/\/seimith.io"],"url":"https:\/\/seimith.io\/pt\/author\/seimithsuth\/"}]}},"_links":{"self":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":1,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/posts\/70\/revisions\/71"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/media\/288"}],"wp:attachment":[{"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seimith.io\/pt\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}