How to truncate tail each line of multiline UILabel - uilabel

NSString *longStr = #"AAAAAAAAAA\nBBBBB\nCCCCCCCCCCCCCCCCCC";
How to make this truncation with UILabel for some label width:
AAA...
BBB...
CCC...

You'll need to use separate labels for each line and use UILineBreakModeTailTruncation.
Hint : Split the text over \n and iterate over the array obtained.
EDIT :
Find the number of characters possible per line in the width of the UILabel and then change your text.
Split over \n -> Iterate over the Strings -> Get SubString of length equal to the max width of the UILabel - some value -> Append trailing dots to that SubString and add the resultant string to tempString -> Create UILabel and assign tempString to it
This would give you the desired result.

Related

Numeric UITextField gives a WordNumeral in UILabel

Sorry in advance how I've written this..
In my Main.StoryBoard I have: UITextField (numeric), UILabel and UIButton.
I would like to:
Click UIButton
Take number from UITextField
Give corresponding 'word' and place in UILabel.
Lets say my numbers are in range from 1-9.
I'm having trouble linking the numbers with words and placing into the UILabel.
Is it best to use an NSArray or perhaps a Case Switch?
CODE
int num = [self.stringEntry.text intValue];
THEN... included NSArray of numbers and words correctly.
self.numberOneList = numberOneArray
self.wordOneList = wordOneArray
if (num <= 0 || num >= 10) {
self.wordLabel.text = #"Try a number between 1 and 9";
} else{
// what would I type here?
You can use NSDictionary in this case. Keys can be numbers (0-9) and values can be respected word. Then just take the value from the textfield and check it against the dictionary using valueForKey:.

Objective-C, correctly separate columns in CSV by comma

I have an Excel file that I exported as a CSV file.
Some of the data in the columns also have commas. CSV escapes these by putting the string in the column in quotes (""). However, when I try to parse it in Objective-C, the comma inside the string seperates the data, as if it were a new column.
Here's what I have:
self.csvData = [NSString stringWithContentsOfURL:file encoding:NSASCIIStringEncoding error:&error];
//This is what the data looks like:
//"123 Testing (Sesame Street, Testing)",Hello World,Foo,Bar
//Get rows
NSArray *lines = [self.csvData componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
//Get columns
NSArray *columns = [[lines objectAtIndex:0] componentsSeparatedByString:#","];
//Show columns
for (NSString *column in columns) {
NSLog(#"%#", column);
}
//Console shows this:
"123 Testing (Sesame Street
Testing)"
Hello World
Foo
Bar
Notice how "123 Testing (Sesame Street and Testing)" are output as separate columns. I need these to be one. Any ideas?
Any ideas?
Design an algorithm.
At the start of the input you have one of three possibilities:
You have a " - parse a quoted field
You have a , - handle an empty field
Otherwise - parse a non-quoted field
After parsing if there is any more input left then iterate (loop).
You might start with some variables:
NSUInteger position = 0; // current position
NSUInteger remaining = csvData.length; // left to parse
then enter your loop:
while(remaining > 0)
{
get the next character:
unichar nextChar = [csvData characterAtIndex:position];
Now check if that character is a ", , or something else. You can use an if or a switch.
Let's say it's a comma, then you want to find the position of the next comma. The NSString method rangeOfString:options:range will give you that. The last argument to this method is a range specifying in which part of the string to search for the comma. You can construct that range using NSMakeRange and values derived from position and remaining.
Once you have the next comma you need to extract the field, the substringWithRange: method can get you that.
Finally update position and remaining as required and you are ready for the next iteration.
You'll have to handle a few error cases - e.g. opening quote with no closing quote. Overall it is straight forward.
If you start down this path and it doesn't work ask a new question, showing your code and explaining where you got stuck.
HTH

How to take out the first component of an array when it doesn't have any contents

I made a textview that is bulleted only. It works just like a bulleted list in word. Now I am making an array using this code to separate strings by a bullet point (\u2022)
//get the text inside the textView
NSString *textContents = myTextView.text;
//make the array
NSArray *bulletedArray = [textContents componentsSeparatedByString:#"\u2022"];
//print out array
NSLog(#"%#",bulletedArray);
It works perfectly with separating the text into components by bullet points but it keeps the first line that has nothing in it. So when it prints out it looks like this.
"",
"Here is my first statement\n\n",
"Here is my second statement.\n\n",
"This is my third statement. "
The very first component of the array is "" (nothing). Is there a way to avoid adding components that equal nil?
Thanks.
Sadly, this is the way the componentsSeparatedBy... methods of NSString work:
Adjacent occurrences of the separator characters produce empty strings in the result. Similarly, if the string begins or ends with separator characters, the first or last substring, respectively, is empty.
Since you know that the first element will always be empty, you can make a sub-array starting at element 1:
NSArray *bulletedArray = [textContents componentsSeparatedByString:#"\u2022"];
NSUInteger len = bulletedArray.count;
if (bulletedArray.count) {
bulletedArray = [bulletedArray subarrayWithRange:NSMakeRange(1, len-1)];
}
Alternatively, you can use substringFromIndex: to chop off the initial bullet character from the string before passing it to the componentsSeparatedByString: method:
NSArray *bulletedArray = [
[textContents substringFromIndex:[textContents rangeOfString:#"\u2022"].location+1]
componentsSeparatedByString:#"\u2022"];
[[NSMutableArray arrayWithArray:[textContents componentsSeparatedByString:#"\u2022"]] removeObjectIdenticalTo:#""];
that should do the trick
while your bulleted list has always a bullet on index 1, you can simply cut the first index out of the string:
//get the text inside the textView
if (myTextView.text.length > 1) {
NSString *textContents =[myTextView.text substringFromIndex:2];
//make the array
NSArray *bulletedArray = [textContents componentsSeparatedByString:#"\u2022"];
//print out array
NSLog(#"%#",bulletedArray);
}
of course you should avoid having an empty text, while this would cause an arrayOutOfBounds Exception.

Show Unicode characters in text field

I have a slider for testing and I want the characters represented by the slider position to be shown in a text field. But my text field only shows A-Z and a-z when running my solution below. How can I get Unicode characters into my text field?
- (IBAction) doSlider: (id) sender
{
long long theNum = [sender intValue];
NSString *vc_theString = [NSString stringWithFormat:#"%c", theNum];
[charField setStringValue:vc_theString2];
}
%c is inherited from C and limited to an 8-bit range. You should use %C, which will read in the corresponding argument as a unichar.

How to find caret position in an NSTextView?

I've an NSTextView with with several semi-colon separated strings. I need to find on which of those strings the caret has been placed. How could I do that?
NSInteger insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location;
For Swift 4
let insertionPointIndex = myTextView.selectedRanges.first?.rangeValue.location