Set insets on UILabel












4














I'm trying to set some insets in a UILabel. It worked perfectly, but now
UIEdgeInsetsInsetRect
has been replaced with CGRect.inset(by:) and I can't find out how to solve this.



When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.



My code



class TagLabel: UILabel {

override func draw(_ rect: CGRect) {
let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

super.drawText(in: CGRect.insetBy(inset))
// super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
}

}


Anyone knows how to set the insets to the UILabel?










share|improve this question



























    4














    I'm trying to set some insets in a UILabel. It worked perfectly, but now
    UIEdgeInsetsInsetRect
    has been replaced with CGRect.inset(by:) and I can't find out how to solve this.



    When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.



    My code



    class TagLabel: UILabel {

    override func draw(_ rect: CGRect) {
    let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

    super.drawText(in: CGRect.insetBy(inset))
    // super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
    }

    }


    Anyone knows how to set the insets to the UILabel?










    share|improve this question

























      4












      4








      4







      I'm trying to set some insets in a UILabel. It worked perfectly, but now
      UIEdgeInsetsInsetRect
      has been replaced with CGRect.inset(by:) and I can't find out how to solve this.



      When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.



      My code



      class TagLabel: UILabel {

      override func draw(_ rect: CGRect) {
      let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

      super.drawText(in: CGRect.insetBy(inset))
      // super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
      }

      }


      Anyone knows how to set the insets to the UILabel?










      share|improve this question













      I'm trying to set some insets in a UILabel. It worked perfectly, but now
      UIEdgeInsetsInsetRect
      has been replaced with CGRect.inset(by:) and I can't find out how to solve this.



      When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.



      My code



      class TagLabel: UILabel {

      override func draw(_ rect: CGRect) {
      let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

      super.drawText(in: CGRect.insetBy(inset))
      // super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
      }

      }


      Anyone knows how to set the insets to the UILabel?







      ios swift uiedgeinsets






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 26 '18 at 9:32









      Jacob Ahlberg

      6502428




      6502428
























          4 Answers
          4






          active

          oldest

          votes


















          7














          Please update your code as below



           class TagLabel: UILabel {

          override func draw(_ rect: CGRect) {
          let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
          super.drawText(in: rect.insetBy(inset))
          }
          }





          share|improve this answer























          • I used rect.inset(inset) -> which takes UIEdgeInsets
            – Jacob Ahlberg
            Jun 26 '18 at 9:44












          • @JacobAhlberg, sorry, there are 2 methods :)
            – Sedo
            Jun 26 '18 at 9:45





















          3














          Imho you also have to update the intrinsicContentSize:



          class InsetLabel: UILabel {

          let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

          override func drawText(in rect: CGRect) {
          super.drawText(in: rect.inset(by: inset))
          }

          override var intrinsicContentSize: CGSize {
          var intrinsicContentSize = super.intrinsicContentSize
          intrinsicContentSize.width += inset.left + inset.right
          intrinsicContentSize.height += inset.top + inset.bottom
          return intrinsicContentSize
          }

          }





          share|improve this answer





























            0














            Your "old code" with UIEdgeInsetsInsetRect should work just fine.



            https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby



            Edited #1:



            iOS 12 API changes:



            https://developer.apple.com/documentation/coregraphics/cgrect/1624499-inset?changes=latest_minor






            share|improve this answer























            • I'm using iOS 12.0 Beta, so it's not working
              – Jacob Ahlberg
              Jun 26 '18 at 9:44










            • @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
              – Sedo
              Jun 26 '18 at 9:50





















            0














            For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)



            this:



            override func draw(_ rect: CGRect) {

            let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

            super.drawText(in: rect.inset(by: inset))
            }





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51039489%2fset-insets-on-uilabel%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              Please update your code as below



               class TagLabel: UILabel {

              override func draw(_ rect: CGRect) {
              let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
              super.drawText(in: rect.insetBy(inset))
              }
              }





              share|improve this answer























              • I used rect.inset(inset) -> which takes UIEdgeInsets
                – Jacob Ahlberg
                Jun 26 '18 at 9:44












              • @JacobAhlberg, sorry, there are 2 methods :)
                – Sedo
                Jun 26 '18 at 9:45


















              7














              Please update your code as below



               class TagLabel: UILabel {

              override func draw(_ rect: CGRect) {
              let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
              super.drawText(in: rect.insetBy(inset))
              }
              }





              share|improve this answer























              • I used rect.inset(inset) -> which takes UIEdgeInsets
                – Jacob Ahlberg
                Jun 26 '18 at 9:44












              • @JacobAhlberg, sorry, there are 2 methods :)
                – Sedo
                Jun 26 '18 at 9:45
















              7












              7








              7






              Please update your code as below



               class TagLabel: UILabel {

              override func draw(_ rect: CGRect) {
              let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
              super.drawText(in: rect.insetBy(inset))
              }
              }





              share|improve this answer














              Please update your code as below



               class TagLabel: UILabel {

              override func draw(_ rect: CGRect) {
              let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
              super.drawText(in: rect.insetBy(inset))
              }
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 26 '18 at 10:36









              sohan vanani

              945831




              945831










              answered Jun 26 '18 at 9:40









              Rakesh Patel

              812115




              812115












              • I used rect.inset(inset) -> which takes UIEdgeInsets
                – Jacob Ahlberg
                Jun 26 '18 at 9:44












              • @JacobAhlberg, sorry, there are 2 methods :)
                – Sedo
                Jun 26 '18 at 9:45




















              • I used rect.inset(inset) -> which takes UIEdgeInsets
                – Jacob Ahlberg
                Jun 26 '18 at 9:44












              • @JacobAhlberg, sorry, there are 2 methods :)
                – Sedo
                Jun 26 '18 at 9:45


















              I used rect.inset(inset) -> which takes UIEdgeInsets
              – Jacob Ahlberg
              Jun 26 '18 at 9:44






              I used rect.inset(inset) -> which takes UIEdgeInsets
              – Jacob Ahlberg
              Jun 26 '18 at 9:44














              @JacobAhlberg, sorry, there are 2 methods :)
              – Sedo
              Jun 26 '18 at 9:45






              @JacobAhlberg, sorry, there are 2 methods :)
              – Sedo
              Jun 26 '18 at 9:45















              3














              Imho you also have to update the intrinsicContentSize:



              class InsetLabel: UILabel {

              let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

              override func drawText(in rect: CGRect) {
              super.drawText(in: rect.inset(by: inset))
              }

              override var intrinsicContentSize: CGSize {
              var intrinsicContentSize = super.intrinsicContentSize
              intrinsicContentSize.width += inset.left + inset.right
              intrinsicContentSize.height += inset.top + inset.bottom
              return intrinsicContentSize
              }

              }





              share|improve this answer


























                3














                Imho you also have to update the intrinsicContentSize:



                class InsetLabel: UILabel {

                let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                override func drawText(in rect: CGRect) {
                super.drawText(in: rect.inset(by: inset))
                }

                override var intrinsicContentSize: CGSize {
                var intrinsicContentSize = super.intrinsicContentSize
                intrinsicContentSize.width += inset.left + inset.right
                intrinsicContentSize.height += inset.top + inset.bottom
                return intrinsicContentSize
                }

                }





                share|improve this answer
























                  3












                  3








                  3






                  Imho you also have to update the intrinsicContentSize:



                  class InsetLabel: UILabel {

                  let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                  override func drawText(in rect: CGRect) {
                  super.drawText(in: rect.inset(by: inset))
                  }

                  override var intrinsicContentSize: CGSize {
                  var intrinsicContentSize = super.intrinsicContentSize
                  intrinsicContentSize.width += inset.left + inset.right
                  intrinsicContentSize.height += inset.top + inset.bottom
                  return intrinsicContentSize
                  }

                  }





                  share|improve this answer












                  Imho you also have to update the intrinsicContentSize:



                  class InsetLabel: UILabel {

                  let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                  override func drawText(in rect: CGRect) {
                  super.drawText(in: rect.inset(by: inset))
                  }

                  override var intrinsicContentSize: CGSize {
                  var intrinsicContentSize = super.intrinsicContentSize
                  intrinsicContentSize.width += inset.left + inset.right
                  intrinsicContentSize.height += inset.top + inset.bottom
                  return intrinsicContentSize
                  }

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 26 '18 at 10:11









                  André Slotta

                  9,50711225




                  9,50711225























                      0














                      Your "old code" with UIEdgeInsetsInsetRect should work just fine.



                      https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby



                      Edited #1:



                      iOS 12 API changes:



                      https://developer.apple.com/documentation/coregraphics/cgrect/1624499-inset?changes=latest_minor






                      share|improve this answer























                      • I'm using iOS 12.0 Beta, so it's not working
                        – Jacob Ahlberg
                        Jun 26 '18 at 9:44










                      • @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                        – Sedo
                        Jun 26 '18 at 9:50


















                      0














                      Your "old code" with UIEdgeInsetsInsetRect should work just fine.



                      https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby



                      Edited #1:



                      iOS 12 API changes:



                      https://developer.apple.com/documentation/coregraphics/cgrect/1624499-inset?changes=latest_minor






                      share|improve this answer























                      • I'm using iOS 12.0 Beta, so it's not working
                        – Jacob Ahlberg
                        Jun 26 '18 at 9:44










                      • @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                        – Sedo
                        Jun 26 '18 at 9:50
















                      0












                      0








                      0






                      Your "old code" with UIEdgeInsetsInsetRect should work just fine.



                      https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby



                      Edited #1:



                      iOS 12 API changes:



                      https://developer.apple.com/documentation/coregraphics/cgrect/1624499-inset?changes=latest_minor






                      share|improve this answer














                      Your "old code" with UIEdgeInsetsInsetRect should work just fine.



                      https://developer.apple.com/documentation/coregraphics/cgrect/1454218-insetby



                      Edited #1:



                      iOS 12 API changes:



                      https://developer.apple.com/documentation/coregraphics/cgrect/1624499-inset?changes=latest_minor







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 26 '18 at 9:53

























                      answered Jun 26 '18 at 9:43









                      Sedo

                      419311




                      419311












                      • I'm using iOS 12.0 Beta, so it's not working
                        – Jacob Ahlberg
                        Jun 26 '18 at 9:44










                      • @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                        – Sedo
                        Jun 26 '18 at 9:50




















                      • I'm using iOS 12.0 Beta, so it's not working
                        – Jacob Ahlberg
                        Jun 26 '18 at 9:44










                      • @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                        – Sedo
                        Jun 26 '18 at 9:50


















                      I'm using iOS 12.0 Beta, so it's not working
                      – Jacob Ahlberg
                      Jun 26 '18 at 9:44




                      I'm using iOS 12.0 Beta, so it's not working
                      – Jacob Ahlberg
                      Jun 26 '18 at 9:44












                      @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                      – Sedo
                      Jun 26 '18 at 9:50






                      @JacobAhlberg try this in ios 12 (as far as documentation shows): CGRect.insetBy(inset) Change to rect.inset(by: inset)
                      – Sedo
                      Jun 26 '18 at 9:50













                      0














                      For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)



                      this:



                      override func draw(_ rect: CGRect) {

                      let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                      super.drawText(in: rect.inset(by: inset))
                      }





                      share|improve this answer


























                        0














                        For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)



                        this:



                        override func draw(_ rect: CGRect) {

                        let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                        super.drawText(in: rect.inset(by: inset))
                        }





                        share|improve this answer
























                          0












                          0








                          0






                          For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)



                          this:



                          override func draw(_ rect: CGRect) {

                          let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                          super.drawText(in: rect.inset(by: inset))
                          }





                          share|improve this answer












                          For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)



                          this:



                          override func draw(_ rect: CGRect) {

                          let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

                          super.drawText(in: rect.inset(by: inset))
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 '18 at 12:21









                          Lance Samaria

                          2,21921448




                          2,21921448






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51039489%2fset-insets-on-uilabel%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              Npm cannot find a required file even through it is in the searched directory