UILabel truncation " ..." (space + ...) instead of "..." - iphone

So I am trying to implement UILabel with UILineBreakModeTailTruncation.
So, for example if the text is "StackOverflow is the best website for programmers", it gets truncated as "StackOverflow is the best..."
It needs to be "StackOverflow is the best ..." (Space + ...)
Is there any easy way to implement this ??
Any easy implementations other than subclassing & overriding drawRect, and/or playing around with the frames, (if it character limit frame size, stop it and append " ...") ??
Look forward to interesting implementations !

As far as I know, Apple does not provide any API for this. You have to write your own implementation. I would calculate the width of the UILabel with the current font, see if it exceeds the maximum width, if it does, truncate text, otherwise present text normally.

Related

UILabel.text shortening and appending "..."

I have an UILabel that I want to contain a preview of a long string. It should be a 1-line-label, and should be populated with a maximum number of Characters and then append "..." at the end of the UILabel.text. Now i could do that manually, but I'd really like to calculate all of that stuff and do it dynamically, as the label is a subview of UITableViewCell and when turning the device, it should be possible to stretch it and calculate again.
Any ideas?
P.S. Sorry for any spelling mistakes, I'm not a native speaker..
I'm not sure if I understand your question well, but isn't this supported anyway, if you set the UILabel's lineBreakMode to UILineBreakModeTailTruncation?

Can you split a NSString into an NSArray of strings based on a content size?

Basically i want to pass an NSString and a CGSize (the content size) and get an NSArray of NSStrings that will fit into this content size.
So for instance i have #"something really long" and specify a size CGSizeMake(20, 10), i would get back a NSArray of [#"something", #" really", #" long"] for example.
Anyone have any ideas / sample code?
Not sure this is that straight forward. Is the CGSize enough? Does not the amount of text that can fit inside a contentSize depend on the fontSize too? Not sure you would want to do all this. It can get pretty complex. Dont see the need for all that.
Instead define some comfortable contentSize for your UILabel & truncate the rest or even better opt for Adjust to Fit along with defining a min. font size. With this iOS will try its best to fit your text keeping in mind the min font size. Here's how to do it in IB -
Assuming that you want to break by words (break where there is a space) here is a psuedo-code algorithm:
1) Get Width as parameter
2) Declare String temp, int breakPoint, array list
3) For Each (character in String)
Add character to temp String
if (character is space)
set breakPoint to currentIndexInTemp
end if
if (temp String size > Width OR character is newline)
declare String newString
set newString equal to substring of temp(from 0 to breakPoint)
add newString to list
set temp to substring of temp(from breakPoint + 1 to end)
reset breakPoint
end if
end For Each
4) if (length of temp > 0)
add temp to list
end if
5) return list
I'm sorry if that's terrible pseudo-code, I don't write pseudo-code often (actually, at all) and I'm not familiar enough with Objective-C to write it in the language.
NOTE
The breakPoint is the last occurrence of a space, or break in
characters, if the length is too long then you want to cut at the
breakPoint to prevent chopping words. This is the index in the
temp String only, not the String you're breaking up.
There may be a simpler way to to do this in Objective-C, I don't know, this algorithm is based off a function I wrote in Java to break a string into 80 character lines.
EDIT
Added New Line testing. In my example, if it's a new line then break it there whether it's too long or not. Also, when you break the String into an array (the second if) you'll want to reset the breakPoint variable. I'd also agree that for overly large portions of text this will add overhead, I've not had issues with text-processing of several hundred characters. Although that was done on a desktop/laptop program.

How to display a lot of read only text in GWT?

I have a requirement to display a somewhat large amount of text, read only to the user. It can be up to a maximum of 500 characters, which isn't excessive, but it's still a lot. Since it's read only I was thinking of a label a versus text area box, if it can handle that much. Is there a better way to do this than I'm not aware of?
Thanks,
James
Label works fine. Just remember that the default css for white-space collapses whitespace rather aggressively. If your text includes line breaks you may want to switch to pre or pre-wrap.
The most straightforward (if not necessarily most correct) way to do that is:
Label myLabel = new Label();
myLabel.getElement().getStyle().setProperty("whiteSpace", "pre");
Note the Camel Case on the CSS attribute.
Either a Label or a TextBox will definitely be able to handle 500 characters.
Think of all the blog posts, Wikipedia articles, Stack Overflow questions, longer than that that have been written. They were all composed in a text box and displayed in a div. You'll be fine.
500 chars is no big deal so it will be ok. Label is ultimately calling element.innerHTML = text which is a browser-native Javacript function that can handle any amount of text.

Get text from UITextView

Is it possible to get the first line of text from a UITextView. I have looked through the UITextView and NSString Class References and can't find any methods that could accomplish this.
The only way to accomplish this is through a process of iteration using the multi line text methods detailed in "NSString UIKit Additions Reference". Extract the first few words from your string and start compositing these gradually until the " sizeWithFont:constrainedToSize:lineBreakMode:" returns a size which height is higher than that of a line with just 1 word.

How to make the 1st line in certain amount of text in a Label bold?

I have about 400 character length string with a heading called Details. In this 'Details' is to be bold and of fontsize 19. While all the remaining text starts in the next line and should be of fontSize 18 like the contents. How can I do all this by using a UILabel?
Plz help me...
You can't do it with a stock UILabel as of SDK 3.2. You can create an NSAttributedString that specifies particular styles (like bold) for particular parts of the string, but there isn't a simple way to actually render it. This is unlike In Max OS X, which has NSAttributedString(AppKitAdditions), which allows you to draw an attributed string in a single line of code. If you want to render it, you can, but you'll have to delve into one of the lower level APIs (such as Core Text).
You're probably much better off using two UILabels. Make the first one bold, and make the second one cover multiple lines.
Since UILabel does not support attributed strings, you have two options:
Subclass UILabel and override -drawRect:
Create 2 UILabels, one of which will display the heading, the other showing the rest of your text. You can choose to wrap the two labels into your own view if necessary.
You could use a UIWebView with an HTMLString of <strong>First part</strong> second part