UILabel Word Wrap / Character Wrap - objective-c

I have a UILabel with the lineBreakMode set to UILineBreakModeWordWrap. This works fine, except when I have a long slab of text with no spaces. In this case it does not wrap the long slab of text but instead just cuts it off once it reaches the right-hand end of the UILabel frame. How can I tell the UILabel that it should wrap on a character boundary in this situation only (essentially equivalent to the UILineBreakModeCharacterWrap setting, but only for those long slabs of spaceless text).
Thanks in advance!

For anyone else having this same issue, I ended-up solving it by using a UITextView instead of a UILabel. This was the best solution for two reasons:
It doesn't require any custom behaviour to determine whether the text contains spaces and change the line break mode of the UILabel to/from word/character wrap.
More importantly, there is an edge case whereby you may have normal words (that you want to wrap on word boundaries) plus extra long text (which you need to wrap on character boundaries). Short of writing some kind of logic to insert a space into that extra long text (at the correct position) to force a "fake word wrap" I can't see any way to handle wrapping on words and characters, depending on the situation, within the one UILabel. The UITextView handles this situation automatically, breaking on word boundaries or character boundaries as necessary.
For specifics on how I am doing this, I have a one line UITextView with editing and scrolling disabled. I also set the .contentInset to remove the padding making it look (to the unsuspecting eye) just like a UILabel. I am then using the sizeWithFont:constrainedToSize:lineBreakMode: method to determine the frame of the rendered text, and adjusting the frame of the UITextView accordingly so that it exactly fits the text.
Hope that this helps!

One way to do this is to set the Lines property in IB.
or from code do this -
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 3;
So when you set the number of lines as 3 the text wraps till that many lines are occupied.

You have to do it yourself, there's no option for 'use word wrapping except when I don't want you to' :)
If a word was too long you could insert spaces into it before you display it to help the label know where to wrap?

dean is right. If you want it you have to do it manually. The below will code will wordwrap a label even though it doesn't spaces. This may help
NSString *someText = yourLabel.text;
//Check if the text contains spaces
//The method in the below if condition is user defined and you have to define one. lol
if(![self textContainsSpaces:someText])
{
//Do the word wrap manually
CGSize constraintSize;
constraintSize.width = 165;
constraintSize.height = 165;
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
CGRect rect = CGRectMake(yourLabel.frame.origin.x, yourLabel.frame.origin.y, 165, (stringSize.height+10));
yourLabel.frame = rect;
}

Related

UITextView mark text

I have a UITextView and would like to offer the user a chance to mark text within this text view.
I have tried to use UITextPosition but it is not precise enough and I do not know which text has being marked.
UITextPosition *tapPos = [textView closestPositionToPoint:touchPosition];
UITextRange *wr = [textView.tokenizer rangeEnclosingPosition:touchPosition withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];
This solution also does not solve the next step of dragging along the words in order to mark more than one word.
I thought of something like the makers of Lightly did.
http://lightlyapp.com

UILabel cutting off text

I have a bit of a strange problem. I have a UITableView controller. Inside the UITableViewCells are four profile images for the user and below each picture is a UILabel to display the name. All the labels are the same size and all the constraints are equally set up. The maximum username length is 12 characters and it displays this correctly, not text cut. I have one username though that is also 12 characters long and it is cutting off 3 characters. It should surely fit if all the other 12 character names do. Could anyone give me any pointers to why this might be happening?
Thanks
As mentioned, unless you use a monospaced font, 12 characters are going o occupy a varying amount of space.
The easiest thing to do in this case is to set the adjustsFontSizeToFitWidth to YES. This will scale the text so that it fits the width of its container.

How to change font color in MasterDetail NSMutableArray row

Of the several questions I have reviewed here on the subject of changing font colors, I assume there is no way to do this, but I will try again.
I start with a fresh copy of the MasterDetail template in xcode. The array to be loaded onto the Master view is defined as NSMutableArray *_objects;
I replace the statement that normally inserts the date/time into the row with statements to put in a sentence, "Help me display this in red as shown below:
//[_objects insertObject:[NSDate date] atIndex:0]; // removed this line and replaced with next two.
NSString *loadme = [[NSString alloc] initWithFormat:#"Help me display this in red"];
[_objects insertObject:loadme atIndex:0];
So, the question is, how would I change the display of this row on the Master view with red text? thanks for your time and expertise.
Since I think the answer to the above question is that it can't be done. Can you tell me how to put an image on the row before the text. That would be an even better solution. I suspect it has something to do with UITableViewCell, but i have no experience in this area.
There are a couple of ways that you could make this work. One, as per the comment from #CarlVeazey is to use NSAttributedString (like this) so that you can store the text and the colour together.
Another option would be to have another array where you add the colour (UIColor instances) and then when you prepare the cell you can set the label text and textColor from the two arrays.

SizeWithFont of Text With Justification applied?

Is there a way to calculate the size of the justified text in Xcode.I am using a uitextview to show my text and I don't want the text to appear in more than 11 lines.There are methods like
sizewithfont I have used them but even after calculating the size and only populating the
uitextview with the specific amount of text when I apply NSTextAlignmentJustified more lines
are produced than actual ones !
Is there a way to calculate sizewithfont with justification ????

display text with many clickable URLs

I'm using custom NSCell in NSTableView similar to:
http://www.martinkahr.com/2007/05/04/nscell-image-and-text-sample/
I wish to display text with many clickable URL.
can any body throw me in tight direction?
I was trying to do something like:
http://snippets.aktagon.com/snippets/358-How-to-make-a-clickable-link-inside-a-NSTextField-and-Cocoa
but it change NSCell in link, I wish to change only some parts of text into links.
thanks for any help
The line that reads
NSRange range = NSMakeRange(0, [attrString length]);
is picking the part of the text that will be changed into a link. This code makes it the whole string. To make it a subset of the string, just set the range appropriately.