How to make UIView match size of UILabel












0















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

}









share|improve this question

























  • 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
















0















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

}









share|improve this question

























  • 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














0












0








0








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

}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:25









Shruti Thombre

8331722




8331722










asked Jul 20 '14 at 3:08









user3784622user3784622

1251214




1251214













  • 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

















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












1 Answer
1






active

oldest

votes


















1














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)



enter image description here



Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.



enter image description here



EDIT



I have made this project, is this what you are looking for?






share|improve this answer


























  • 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











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%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









1














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)



enter image description here



Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.



enter image description here



EDIT



I have made this project, is this what you are looking for?






share|improve this answer


























  • 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
















1














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)



enter image description here



Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.



enter image description here



EDIT



I have made this project, is this what you are looking for?






share|improve this answer


























  • 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














1












1








1







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)



enter image description here



Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.



enter image description here



EDIT



I have made this project, is this what you are looking for?






share|improve this answer















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)



enter image description here



Then add constraints. You can enter whatever values you like. If you get warnings, you can update the frame or update the constraints.



enter image description here



EDIT



I have made this project, is this what you are looking for?







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















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.




draft saved


draft discarded














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





















































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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks