How to make UILabel wordwrap - iphone

Is there anyway of making a UILabel wrap to the next line or do I have to use a UITextView?

You would need to set the numberOfLines property to 0 and explicitly specify its dimensions, either in IB or programmatically. However, if you have a lot of text to display, I would recommend using a UITextView.

What you want is to adjust your label according to the content's size.
In iOS 6, AutoLayout will work a charm, you should set the label to have numberOfLines = 0
In a non AutoLayout scenario, you will also want to make sure the frame of your UILabel is adjusted to show the content that's on the next line.
Please check out this answer and sample code for that: https://stackoverflow.com/a/8796896/662605

Related

UITextfield text is moving on edit

When my UITextField becomes first responder(on edit) the text is moving down very slightly and the spacing between the characters is increased very slightly. Upon resign first responder(keyboard goes away), the characters move back to the original positioning. At first I thought this was a font issue, as I am using a custom font, but the same thing happens when I use the system font or other custom fonts.
Please check the images below. The first one is with the keyboard down and the second is with the keyboard up. It may be difficult to see, as the variance is small, but the keyboard up state shows the characters moved down and apart slightly.
In xib, set textfield's borderStyle to any value than TextBorderStyleNone . And in code, set textfield's borderStyle as TextBorderStyleNone explicitly.
I could'n figure out why but it seems like initializer bug. It works fine in other code which doesn't initialize UITextField by outlet from xib.
When set textfield's borderStyle as TextBorderStyleNone at xib, the problem still remains even if set borderStyle as TextBorderStyleNone in code. It may be followed by some kind of 'layoutIfNeeded' stuff, which triggered by value change.
Selecting the "adjust to fit" property sets the property adjustsFontSizeToWidth in UITextField.
That has this property (UITextField Apple Documentation):
Normally, the text field’s content is drawn with the font you specify in the font property. If this property is set to YES, however,
and the contents in the text property exceed the text field’s bounding
rectangle, the receiver starts reducing the font size until the string
fits or the minimum font size is reached. The text is shrunk along the
baseline.
The default value for this property is NO. If you change it to YES,
you should also set an appropriate minimum font size by modifying the
minimumFontSize property.
I would try first unchecking this and seeing if that would stop it, and then follow the documentation advice and set the minimumFontSize property lower otherwise, which may also be causing the problem.
This issue could possibly be a result of the appearance of the blue "now editing" cursor. The baseline of a text (reference) is the bottom of the text. Your text is shrinking "along the baseline" by pushing down, which leans more strongly in the direction of it being a minimumFontSize/adjustsFontSizeToWidth issue.
If you are forcefully making UITextField first responder, do it in viewDidAppear: instead of viewDidLoad... This solved the problem for me...
Checking OFF the "Clip to Bounds" of textField in my storyboard works fine for me.
You have probably sized your UITextField to be slightly too small height-wise. Try manually resizing it so it's a pixel or two taller. Or select it in the XIB editor and use "Size to fit content" in the Editor menu. That ought to set the text field to the right size so that this text shifting doesn't happen.
Setting the Min Font Size property equal to the font size in Interface Builder worked for me
Changing the vertical alignment under control in the storyboard did it for me.
My issue was connected with wrong alignment of UIStackView.
When you put UITextField into UIStackView, make sure you set its alignment correctly, otherwise auto-layout won't figure out how to position your view right.
In my case, I had to set it to .leading.
stackView.alignment = .leading
You should set Font property earlier than Text property.

how to make UILabel autosize text in storyboard (or interface builder), NOT programmatically

Every time I click-and-drag a UILabel to storyboard, and then add text to Text field, the text is cut off, so I then have to click-and-drag the UILabel to stretch it big enough so that the text appears.
I can't figure out how to make the UILabel automatically get big enough to fit the text.
This would save time while designing new views.
On any element in Interface Builder you can select the element and hit...
Editor > Size to Fit Content (keyboard shortcut: CMD+=)
This will do a "sizeToFit" on the selected element. Labels will fit their text size, image view will resize to the image size, etc...
In Xcode 6.1, I had to set Content Compression Resistance Priority to a higher value, likely because I have other constraints that were conflicting, or causing the sizeToFit option to be disabled.
Before:
After:
For the text label:
Set Lines to 0
Set Height to Greater than or equal to 30 (or other value)
Now the height will be adjusted based on the lines number.
Starting with iOS 6, there's a new API, available in Interface Builder as well called 'Auto Layout'. You can specify some contraints and UIKit will resize / move your views based on those contraints. If you don't want to use Autolayout you can use the autoresizeMask property of UIViews (which is settable in Interface Builder as well).
Check out the Autolayout Guide!
To resize the views in Interface Builder as you build them, see the reply from #Fogmeister

Textlabel in tableview cell doesn't resize after refresh

When I initialize a textlabel from a static tableview cell in viewDidLoad, everything is displayed as it should be. However when I change the textvalue some moments later after the press of a button, the text is clipped if it was bigger than the original text set in viewDidLoad. When the new text is shorter, it's being displayed correctly.
Somebody knows a solution for this problem?
-- EDIT --
Solved with:
try calling the label's setNeedsLayout method after changing the text value - that may resize the label. –
Set the adjustsFontSizeToFitWidth property of the label to YES. It will make the font smaller when text becomes longer. Also set the minimumFontSize property to a smaller value than the default.

UITableView create a badge on the right of every cell

I want to create a table view like this one with a badge on the right. Can someone suggest me how can I do to get this result?
thanks
I'm using greate open source project TDBadgedCell from Tim Davies for that purposes. It is very simple to use and configure.
Get it here: https://github.com/tmdvs/TDBadgedCell
AFAIK, this is not available by default. You have to customize your cell.
Create a UILabel.
Set textAlignment as UITextAlignmentCenter.
Change its cornerRadius.
Add a drop shadow using shadowColor, shadowOffset(a positive offest) ,etc.,
Set label's text.
Resize label's width to fit the text.
Set it as cell's accessoryView.

UITextView with 3 lines automatically scrolls up when I enter <screen capture>

I have a UITextView that i use for typing some text
(i have added it with Interface builder)
i have made the height 3 font lines high (so in my case the frame height is 47) so i can have 3 lines of text
what i want it to do is type until line3 and the go up if i goto line 4
but what it does is whenever i type enter it automatically goes up so i'm constantly typing at the top of my uitextview
ive put a capture online that show it more cleary .. even when i click a line it goes to the top!
Click here for the screen capture
Write [textview setContentInset:UIEdgeInsetsZero] in textviewbeginediting delegate method or either at the ViewDidLoad method
Hope this will help you.....
It looks like the trick is to make the font size bigger
txtView.font = [UIFont systemFontOfSize:14];
when i make my font size 2 points bigger it just types normal instead of going up every line.
I struggled with this for weeks and finally found out that all I had to do was to set the UITextView height to double (or whatever you want), set its background color to ClearColor and you are all set. You just have to play with the height and you will get it. In my case the UITextView was in a tablecell..first when I increased the height it started showing on top of the cell below the current cell. Thats when ClearColor did the magic for me.