How to make UIView match size of UILabel
I have a UILabel that has auto layout applied to it as well as its preferredWidth changed to ensure it is formatted properly. I want a UIView to be 'underneath' it and match the size of the label. Setting the frame.size of the UIView to the label.frame.size does not work as it does not consider the auto layout that is occurring.
I am thinking there must be a way to match the constraints of the UILabel and then adjust size after that. However I don't think this can work, as the UILabel's formatting is not relevant to the separate UIView.
Note: the text is known past runtime because it is dependent on information from a web server.
Relevant Code:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel.numberOfLines = 0
cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 35
preferredWidth = cell.messageLabel.preferredMaxLayoutWidth
cell.messageLabel.text = friends[indexPath!.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.setNeedsDisplay()
//cell.msgBubble.frame.size.width = preferredWidth
//cell.msgBubble.
cell.msgBubble.layer.masksToBounds = true
cell.msgBubble.layer.cornerRadius = 10.0
return cell
// Configure the cell...
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var sampleLabel = UILabel()
sampleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
sampleLabel.numberOfLines = 0
sampleLabel.preferredMaxLayoutWidth = preferredWidth
sampleLabel.text = friends[indexPath!.row]
sampleLabel.sizeToFit()
sampleLabel.setNeedsDisplay()
return sampleLabel.intrinsicContentSize().height + 10
}
ios swift uilabel
add a comment |
I have a UILabel that has auto layout applied to it as well as its preferredWidth changed to ensure it is formatted properly. I want a UIView to be 'underneath' it and match the size of the label. Setting the frame.size of the UIView to the label.frame.size does not work as it does not consider the auto layout that is occurring.
I am thinking there must be a way to match the constraints of the UILabel and then adjust size after that. However I don't think this can work, as the UILabel's formatting is not relevant to the separate UIView.
Note: the text is known past runtime because it is dependent on information from a web server.
Relevant Code:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel.numberOfLines = 0
cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 35
preferredWidth = cell.messageLabel.preferredMaxLayoutWidth
cell.messageLabel.text = friends[indexPath!.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.setNeedsDisplay()
//cell.msgBubble.frame.size.width = preferredWidth
//cell.msgBubble.
cell.msgBubble.layer.masksToBounds = true
cell.msgBubble.layer.cornerRadius = 10.0
return cell
// Configure the cell...
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var sampleLabel = UILabel()
sampleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
sampleLabel.numberOfLines = 0
sampleLabel.preferredMaxLayoutWidth = preferredWidth
sampleLabel.text = friends[indexPath!.row]
sampleLabel.sizeToFit()
sampleLabel.setNeedsDisplay()
return sampleLabel.intrinsicContentSize().height + 10
}
ios swift uilabel
Just set theUIView
's constraints to exactly equal the size and position of theUILabel
. If there are no other constraints on theUIView
it will always align with theUILabel
perfectly.
– Aaron Brager
Jul 20 '14 at 3:27
add a comment |
I have a UILabel that has auto layout applied to it as well as its preferredWidth changed to ensure it is formatted properly. I want a UIView to be 'underneath' it and match the size of the label. Setting the frame.size of the UIView to the label.frame.size does not work as it does not consider the auto layout that is occurring.
I am thinking there must be a way to match the constraints of the UILabel and then adjust size after that. However I don't think this can work, as the UILabel's formatting is not relevant to the separate UIView.
Note: the text is known past runtime because it is dependent on information from a web server.
Relevant Code:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel.numberOfLines = 0
cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 35
preferredWidth = cell.messageLabel.preferredMaxLayoutWidth
cell.messageLabel.text = friends[indexPath!.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.setNeedsDisplay()
//cell.msgBubble.frame.size.width = preferredWidth
//cell.msgBubble.
cell.msgBubble.layer.masksToBounds = true
cell.msgBubble.layer.cornerRadius = 10.0
return cell
// Configure the cell...
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var sampleLabel = UILabel()
sampleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
sampleLabel.numberOfLines = 0
sampleLabel.preferredMaxLayoutWidth = preferredWidth
sampleLabel.text = friends[indexPath!.row]
sampleLabel.sizeToFit()
sampleLabel.setNeedsDisplay()
return sampleLabel.intrinsicContentSize().height + 10
}
ios swift uilabel
I have a UILabel that has auto layout applied to it as well as its preferredWidth changed to ensure it is formatted properly. I want a UIView to be 'underneath' it and match the size of the label. Setting the frame.size of the UIView to the label.frame.size does not work as it does not consider the auto layout that is occurring.
I am thinking there must be a way to match the constraints of the UILabel and then adjust size after that. However I don't think this can work, as the UILabel's formatting is not relevant to the separate UIView.
Note: the text is known past runtime because it is dependent on information from a web server.
Relevant Code:
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MessageTableViewCell
cell.messageLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.messageLabel.numberOfLines = 0
cell.messageLabel.preferredMaxLayoutWidth = cell.frame.size.width - 35
preferredWidth = cell.messageLabel.preferredMaxLayoutWidth
cell.messageLabel.text = friends[indexPath!.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.setNeedsDisplay()
//cell.msgBubble.frame.size.width = preferredWidth
//cell.msgBubble.
cell.msgBubble.layer.masksToBounds = true
cell.msgBubble.layer.cornerRadius = 10.0
return cell
// Configure the cell...
}
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
var sampleLabel = UILabel()
sampleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
sampleLabel.numberOfLines = 0
sampleLabel.preferredMaxLayoutWidth = preferredWidth
sampleLabel.text = friends[indexPath!.row]
sampleLabel.sizeToFit()
sampleLabel.setNeedsDisplay()
return sampleLabel.intrinsicContentSize().height + 10
}
ios swift uilabel
ios swift uilabel
edited Nov 26 '18 at 13:25
Shruti Thombre
8331722
8331722
asked Jul 20 '14 at 3:08
user3784622user3784622
1251214
1251214
Just set theUIView
's constraints to exactly equal the size and position of theUILabel
. If there are no other constraints on theUIView
it will always align with theUILabel
perfectly.
– Aaron Brager
Jul 20 '14 at 3:27
add a comment |
Just set theUIView
's constraints to exactly equal the size and position of theUILabel
. If there are no other constraints on theUIView
it will always align with theUILabel
perfectly.
– Aaron Brager
Jul 20 '14 at 3:27
Just set the
UIView
's constraints to exactly equal the size and position of the UILabel
. If there are no other constraints on the UIView
it will always align with the UILabel
perfectly.– Aaron Brager
Jul 20 '14 at 3:27
Just set the
UIView
's constraints to exactly equal the size and position of the UILabel
. If there are no other constraints on the UIView
it will always align with the UILabel
perfectly.– Aaron Brager
Jul 20 '14 at 3:27
add a comment |
1 Answer
1
active
oldest
votes
I assume you have added enough constraint for the label. Now we need to add constraints for the view to resize according to the label.
Select both the label and the view, you can do it in the side bar in Xcode. (cmd + mouse click)
Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.
EDIT
I have made this project, is this what you are looking for?
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
|
show 1 more 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%2f24846981%2fhow-to-make-uiview-match-size-of-uilabel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume you have added enough constraint for the label. Now we need to add constraints for the view to resize according to the label.
Select both the label and the view, you can do it in the side bar in Xcode. (cmd + mouse click)
Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.
EDIT
I have made this project, is this what you are looking for?
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
|
show 1 more comment
I assume you have added enough constraint for the label. Now we need to add constraints for the view to resize according to the label.
Select both the label and the view, you can do it in the side bar in Xcode. (cmd + mouse click)
Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.
EDIT
I have made this project, is this what you are looking for?
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
|
show 1 more comment
I assume you have added enough constraint for the label. Now we need to add constraints for the view to resize according to the label.
Select both the label and the view, you can do it in the side bar in Xcode. (cmd + mouse click)
Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.
EDIT
I have made this project, is this what you are looking for?
I assume you have added enough constraint for the label. Now we need to add constraints for the view to resize according to the label.
Select both the label and the view, you can do it in the side bar in Xcode. (cmd + mouse click)
Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.
EDIT
I have made this project, is this what you are looking for?
edited Jul 20 '14 at 3:50
answered Jul 20 '14 at 3:27
ukimukim
1,550816
1,550816
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
|
show 1 more comment
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
Normally this would solve the issue if the layout was understood at runtime. However, the text in the UILabel is determined past runtime, so my UIView is basically stretched to the default size of the UILabel, rather than to size that the UILabel is when there is content inside of it (which is formatted using the code in that first method)
– user3784622
Jul 20 '14 at 3:33
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I have made a small project. If I understand you question correctly, this would be helpful. I have added it at the bottom of my answer.
– ukim
Jul 20 '14 at 3:43
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I guess maybe you should set the constrains yourself, instead of calling sizeToFit(). I used an alternative method in the project above.
– ukim
Jul 20 '14 at 3:45
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
@user3784622 I have modified the project. If you have downloaded it before seeing this message, please download again.
– ukim
Jul 20 '14 at 3:53
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
This helped me a ton, thank you!
– user3784622
Jul 20 '14 at 4:12
|
show 1 more 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.
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%2f24846981%2fhow-to-make-uiview-match-size-of-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
Just set the
UIView
's constraints to exactly equal the size and position of theUILabel
. If there are no other constraints on theUIView
it will always align with theUILabel
perfectly.– Aaron Brager
Jul 20 '14 at 3:27