UILabel.text shortening and appending "..." - ios5

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?

Related

Star symbol too small in a dialer-like view

I'm trying to create a dialer-like application:
I'm using [UIFont systemFontOfSize:33]. The problem is that the Asterisk symbol is too small in comparison to the numbers and '#'.
I printed 123*# in all 61 available iOS6 fonts and the star is smaller than other chars in all of them.
Does somebody have an idea how to solve this?
One thing I tried is changing font size only for * button. That works, but when I hit this button it appears small [off course] in the input above...
Hope my Question is clear.
Thanks.
Use a different character for the display. In Xcode, click on the Edit menu and select Special Characters. When the character viewer appears, type "asterisk" into the search field. Try one of the many other related symbols.
Depending on how you do this, you may need to replace the used symbol with a proper asterisk internally to use the result in a tel URL.
You can use attributedString and change the font size to big enough of all asterisks

LED Display simulation Xcode

I have been very long looking for the answer to my question on the web, but I didn't found anything helpful so I decided to ask you.
Basically I want to Programm a app like how this video shows. Another big deal for me is - how can I change the text size ?
For each character I should make a array of boolean to determine if the led pin should be on or off. I think this is the smartest way for displaying text in fix text size.
Do I have to make an array for every character in every text size I want to get this function working ?
Essentially you are making a Font Map for each font you wish to display on your large LED display. Just like fonts on a regular display, Im afraid the answer is YES, you will have to make essentially a map file for every font you wish to support.
It may not have to be true booleans however, you may be able to get away with a much more compact version of the booleans by storing multiple boolean bits in a single byte, short, or int, etc...

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.

UITableViewCell newline characters

Is there any way to get newlines to be converted to actual linebreaks in UITableViewCell?
Right now they show up as \r\n (they come from a sqlite3 db) and just get displayed as such.
If I search and replace \r\n with actual return characters then it works fine, but I'm wondering if three is a proper way to do this?
Please don't suggest using other View types, the app is basically 100% complete save for this last bug and I don't want to re-test everything.
thanks
T
edit: Solution in case anyone has a similar issue:
cellText = [cellText stringByReplacingOccurrencesOfString:#"\\r\\n" withString:#"\n"];
Olaf got me on the right path, thanks.
IIRC on the Mac newline is \n, but this is not the question.
If you are seeing the four characters "\r\n" on the screen then I assume the string coming from db is escaping the backslashes. Thus when you do the manually rewrite the (escaped) string \\r\\n is converted into \n.
The standard cell is using an embedded UILabel anyway, so rewriting your cell with a custom cell using another UILabel would not change much. ;)
I just finished fixing this very issue in my code using:
myString = [[myString
stringByReplacingOccurrencesOfString:#"\\\\n" withString:#"\n"]
stringByReplacingOccurrencesOfString:#"\\\\r" withString:#""];
Hope that helps

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