How to set different width of elements [iPhone-dev] - iphone

Look this pic:
The usernames are UIButton. How can I put a UILabel (with the comment text) just after the UIButton (with the username) ?
The UIButton username's is dynamic. How can I do this?

That looks like a UITableView, if that's the case you should subclass UITableViewCell. If it isn't, I would
consider making it one
build a container class that (or make your subclassed UITableViewCell such that it) contains both a UIButton and a UILabel and dynamically position the text inside the label according to the size of the button (for instance, adding the right number of spaces to the beginning of the text).

You're going to have to use Core Text or web views to achieve this. There's really not a super easy way to do this. It's simple enough to place view next to each other based on their calculated sizes, but to have text wrap to multiple lines with embedded font style variations and attachments (i.e., images), you're going to have to read up on Core Text or use XHTML in web views.

Related

Iphone user input structure

I am new to iPhone development and I am looking at exactly what is shown in the picture to allow users to write input.
But I cannot find what exactly is it! Are those some special kind of UITextFields? Are they a special kind of UITableView? What is it?
This is a UITableView with custom UITableViewCell's that contain UITextFields. You can easily do this by loading your UITableViewCell from custom XIB files, in which you've dropped UITextFields. As far as the "hints" you see in each field, that's the "Placeholder" property of UITextField.
Those are grouped tableviews with custom table cells that contain textfields and single line etched separators. Make sure to set the style to "grouped"
The above basically consist of UItableviewCell with a Uitextfield within it.
If you are on ios5 (which i assume you will be), you can add UItableviewcontroller from you Interface Builder and from there, taking the advice to set the style of the table to "grouped".
you should have the similar outlook of the above for the background.
In here, if you select a cell you should have the option to set a few different style to your uitableview cell.
If none of the default styles works for you, just select custom and add your own uitextfield or uibutton.
Note: There's a bit of difference between prototype cell.

How to design rounded boxes and separators (like the App Settings view)?

I am wondering what is the good way to design interfaces such as the one in the Settings view on an application, for instance :
What I want to do is the nice round rectangle to separate categories and horizontal line separators between categories, I can have a label, text field, slider or any other control in each line...
Do we need to use an image in the background, that seems quite dirty to me, and I cant find any control in IB that seem to do the same kind of layout.
So, how is this done?
Thanks!
Use a UITableView and set it's style to UITableViewStyleGrouped. Remember that the standard UITableViewCell's will just let you show some text and you may need to create custom UITableViewCell's to achieve more (for example, a on-off switch).
If you wan't to customise it you can add a background image. To do this, place a UIImageView behind the UITableView and make sure you set the UITableView background colour to clear:
theTableView.backgroundColor = [UIColor clearColor];
To seperate the categories make use of the "sections".
Basically, you can use grouped table.You can have sections with different/same number of rows.

UIScrollView, UILabel, Tickers

I've created a monochrome ticker [think stock ticker] to display information the user encounters within my app. It's a wide and narrow instance of UIScrollView whose content is filled by an instance of UILabel with the relevant text. The parent view is its own delegate and takes care of the horizontal scrolling animation. The UILabel instance is set to one line only.
I'd like for the text to be in two colors (red and black) where necessary, and possibly plain and italics again where necessary. Of course UILabel is not capable of doing this, and neither is UITextView.
The obvious thing to try is replacing the UILabel instance with an instance of UIWebView and send it an html-string to format the text. However the results are not yet quite as satisfying as with UILabel - the text has margin offsets, for instance, and it needs to be displayed on one line.
Another solution, which works but I'm not satisfied with, is to have the UIScrollView display a concatenated sequence of UILabel instances each with the appropriate font or text color.
With regard to the UIWebView solution what do you advise? For instance, how would you configure an instance of a UIWebView to visually and faithfully resemble the UILabel?
check out the fancy label example at http://furbo.org/2008/10/07/fancy-uilabels/
if it helps you.

How to arrange labels in a flowlayout manner?

How do I arrange some UILabels and/or UIButtons of a variable length? I just want to add them to a UITableViewCell and they should arrange in a left-to-right flow, much like lines of text in a paragraph.
I only found possibilities to create lables with a fixed size and position using "initWithFrame:...". Same seems to be true for Interface Builder, as far as I can tell. Any solution is appreciated no matter if it's done in code or using a custom cell XIB-file.
UITableViewCell, UILabel, and UIButton are all subclasses of UIView and the documentation for UIView says:
Layout and subview management
A view may contain zero or more subviews.
Each view defines its own default resizing behavior in relation to its parent view.
A view can manually change the size and position of its subviews as needed.
So, it is certainly possible to do.
You can create your labels and buttons using initWithFrame: with the argument CGRectZero and then resize them (based on the text or whatever) using setBounds: or setFrame: (because right now you're just going to set the size of the view). Then, add these views as subviews of the cell's contentView.
Then, in a custom subclass of UITableViewCell you can implement your solution by overriding the default behavior (which does nothing) of layoutSubviews: to set the origin field of the subview's frames (i.e., CGRect) that will position the subviews in the cell's content view (the size has already been set). You may need to call setNeedsLayout: or layoutIfNeeded:.
This is really a rough outline of how it is possible to implement a solution because there are a lot of details left out. For example, if you resize a button based on the the text of the titleLabel you'll probably want to pad some to the width and height otherwise the button will be the size of the label and will look odd. In the layoutSubviews: method there could be a fair amount of logic to layout the labels and buttons the way you want (e.g., it would be simpler if all the subviews of a cell where of the same type such as all labels) esp. if the subviews could wrap to a new line.
For multiline UILabels to get the width and height you should use the NSString method sizeWithFont:constrainedToSize:lineBreakMode: you can then use the sizes you get to lay everything out where it needs to be.
I want to do the same thing to allow users to enter tags into a text field - a bit like how when you type in an email address, the address gets converted into a blue tag (the the users name in when that users email address is already in your contacts list). Haven't written it yet, but will be happy to share with you once I do. I can't commit to how long it I will take to write this unfortunately. However if no one else has code they can share and you need to get the job done quickly - Just as a tip, consider this:
Create tag view objects where each object knows the size of the parent text field/tag container view and where each tag object has a utility method which a further tag object can use to insert itself at the right position. This approach makes it easy to manage the view and relayout tags using a simple iteration flow.
Hi
If you still need an answer...
To get the size of text (and to then calculate the frame of the UILabel/UIButton etc) use the sizeWithFont: NSString function which will give you the width/height of a string of text using a specified font.
There is a little bit of maths that you'll need to do to work out the best fit, where to place the UILabels, and the spacing, but you will have the data that you need to do it.
Hope this helps!

How to do edit-in-place in a UITableView?

Is there a standard way to set up a table to allow editing-in-place, kind of like this:
I only need editable text at the moment, but I might need UISwitches or UISliders in the future.
Yep. Just add a UITextField, with its font and textColor set to appropriate values, as subviews of the table cell's contentView. You probably want to give the field a tag as well, so that you can easily grab a reference to it using the contentView's -viewWithTag: method.
With short forms you can get away with keeping an array of cells, one for each field, and handing them off to the table view without going through the -dequeueReusableCellWithIdentifier: mechanism, but if you've got a lot of stuff to enter then it gets more complicated. In that case, you probably want to assign a different reuse identifier to each type of cell—one for text-field cells, one for switch cells, one for slider cells, etc. Once you've dequeued or created the row's cell, you'd then grab its control from the content view (as above) and set its value from wherever you've stored that.
Edit in-place using a "Content: Static Cells" and make it look like a default styled UITableviewCell.
In UITableView create two cells: one Custom and one Subtitle Styled.
Copy the "title" label corresponding to the Subtitle styled UITableViewCell to the Custom UITableViewCell.
In Custom UITableViewCell, drop a UITextField. Copy in this the style parameters from the Detail UILabel from Subtitle Styled UITableViewCell in this UITextField.
Do the necessary arrangements.
The looks pretty equal, aren't they?
Now, do the IBoutlet stuff.