Set insets on UILabel
I'm trying to set some insets in a UILabel. It worked perfectly, but now
has been replaced with
UIEdgeInsetsInsetRectCGRect.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
add a comment |
I'm trying to set some insets in a UILabel. It worked perfectly, but now
has been replaced with
UIEdgeInsetsInsetRectCGRect.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
add a comment |
I'm trying to set some insets in a UILabel. It worked perfectly, but now
has been replaced with
UIEdgeInsetsInsetRectCGRect.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
I'm trying to set some insets in a UILabel. It worked perfectly, but now
has been replaced with
UIEdgeInsetsInsetRectCGRect.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
ios swift uiedgeinsets
asked Jun 26 '18 at 9:32


Jacob Ahlberg
6502428
6502428
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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))
}
}
I usedrect.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
add a comment |
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
}
}
add a comment |
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
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
add a comment |
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))
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
}
}
I usedrect.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
add a comment |
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))
}
}
I usedrect.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
add a comment |
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))
}
}
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))
}
}
edited Jun 26 '18 at 10:36


sohan vanani
945831
945831
answered Jun 26 '18 at 9:40


Rakesh Patel
812115
812115
I usedrect.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
add a comment |
I usedrect.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
add a comment |
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
}
}
add a comment |
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
}
}
add a comment |
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
}
}
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
}
}
answered Jun 26 '18 at 10:11
André Slotta
9,50711225
9,50711225
add a comment |
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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))
}
add a comment |
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))
}
add a comment |
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))
}
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))
}
answered Nov 19 '18 at 12:21
Lance Samaria
2,21921448
2,21921448
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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