How to organize shortcuts in stream mode?

How can I arrange some UILabels and / or UIButtons of variable length? I just want to add them to the UITableViewCell, and they should line up in the stream from left to right, like text texts in a paragraph.

I just found the ability to create tables with a fixed size and position using "initWithFrame: ...". As far as I know, this is also true for Interface Builder. Any decision is evaluated regardless of whether it was done in code or using a custom XIB cell file.

+7
iphone uikit layout
source share
4 answers

UITableViewCell , UILabel and UIButton are all subclasses of UIView , and the documentation for UIView says:

Manage layouts and subheadings

  • A view may contain zero or more views.
  • Each view defines its own resizing behavior in relation to its parent view.
  • A view can manually resize and position its subzones as needed.

Thus, it is certainly possible to do so.

You can create your own labels and buttons using initWithFrame: with the CGRectZero argument, and then resize them (based on text or whatever) using setBounds: or setFrame: (because right now you are just going to set the size of the view). Then add these subviews as subzones of the contentView cell.

Then in a custom subclass of UITableViewCell you can implement your solution by overriding the default behavior (which does nothing) from layoutSubviews: to set the subframe source field (i.e. CGRect) that will position the subviews in the cell content view (the size is already set ) You may need to call setNeedsLayout: or layoutIfNeeded:

This is a really rough outline of how the solution can be implemented, because there are many details left. For example, if you change the size of a button based on the titleLabel text, you probably want to titleLabel some in width and height, otherwise the button will be the size of the label and will look strange. In the layoutSubviews: method, layoutSubviews: may be enough logic to layout labels and buttons the way you want (for example, it would be easier if all the subzones of the cell, where the same type as all the labels) esp. if subviews can be wrapped to a new line.

+1
source share

For multi-line UILabels, to get the width and height, you should use the NSString method sizeWithFont: constrainedToSize: lineBreakMode: you can use the sizes you get to lay out everything where they should be.

+1
source share

I want to do the same so that users can enter tags in the text box - a bit like the way when entering the email address the address is converted to a blue tag (username when the users email address is already in your contact list). It is not written yet, but we will be happy to share it with you as soon as I do it. I cannot take responsibility for how long I will write, unfortunately. However, if someone else does not have code that they can use, and you need to quickly complete the task - just like a hint, think about it:

Create tag view objects where each object knows the field size of the parent text field / container, and where each tag object has a utility method that can use another tag object to insert it into the desired position. This approach simplifies the management of presentation and relay tags using a simple iterative stream.

0
source share

Hi If you still need an answer ... To get the size of the text (and then calculate the UILabel / UIButton frame, etc.) Use the sizeWithFont: NSString function, which will give you the width / height of the line of text using the specified font.

There is some math that you will need to do to work out the best match, where to place the UILabels and spacing, but you will have the data you need to do this.

Hope this helps!

0
source share

All Articles