NSTextField: How to detect when a string is too long? - swift

In my UI I have an NSTextField (used as a label). This text field's string value changes dynamically. I'd like to know when its string value is too long to display. I've configured the text field to truncate its contents:
myLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail
which works fine but I need to know when truncation occurs because in that situation I need to do something else in my UI.

First of all, calculate number of character the label can take (determine the maximum number of characters a UILabel can take).
Suppose, the label takes x characters then check, if(x < label.text.characters.count) { //Do you work.}

Related

OS X App converting textfield with comma causing issues with double value

I am creating an OS X app and when I do this:
var tempVal = 10495.33
tempTextField.doubleValue = self.tempVal
It shows like this: 10,495.33. Notice the comma.
Now when I modify that value to 30,400.34 in the NSTextField and try to assign it back to the doubleValue things get messed up.
tempVal = tempTextField.doubleValue //Now this makes tempVal = 30 instead of 30,400.34
This is all because of the comma. Without the comma things are fine.
I know there is a bad fix where I just remove all commas from the number string but I feel like there is a better/correct way to do this.
The issue is most likely cause by by your system settings. Apparently (do not kwon why) you should enter the numbers in a US like format (using a "." as decimal separator), but the output to the textfield is depending on your system setting. So if you are in Europe most likely you use "," as a decimal separator
you can fix it as follows (you configure the format of the NSTextField, so no need to format the data you enter)
//This codes works in swift 4
//This should be your textfield. It is either programmed or you link it through storyboard
var myTextField = NSTextField()
//This is a formatter. You can set it up how to format the textfield
var myFormat = NumberFormatter()
myFormat.decimalSeparator = "."
myFormat.numberStyle = .decimal
//Now you connect the two
myTextField.formatter = myFormat

SSRS Expression works as cell value expression, but not as background color value expression

I have an SSRS report with a matrix in it, where I needed to display the Growth Percentage in a column group compared to the previous column value. I managed this by using custom code...
DIM PreviousColValue AS Decimal
Dim RowName AS String = ""
Public Function GetPreviousColValue(byval Val as Decimal, byval rwName as string) as Decimal
DIM Local_PreviousColValue AS Decimal
IF RowName <> rwName THEN
RowName = rwName
PreviousColValue = val
Local_PreviousColValue = 0
ELSE
Local_PreviousColValue = (Val - PreviousColValue)/PreviousColValue
PreviousColValue = val
END IF
Return Local_PreviousColValue
End Function
..and then using this as the value expression in the cell..
=Round(Code.GetPreviousColValue(ReportItems!Textbox8.Value,Fields!BusinessUnit.Value)*100,0,system.MidpointRounding.AwayFromZero)
So far so good, this produces the expected value. Now I need to use this expression in a background color expression to get a red/yellow/green but in that capacity it fails.
The background color expression looks like this: =IIF(ROUND(Code.GetPreviousColValue(ReportItems!Textbox9.Value,Fields!Salesperson.Value)*100,0,System.MidpointRounding.AwayFromZero)<=-5,"Red"
,IIF(ROUND(Code.GetPreviousColValue(ReportItems!Textbox9.Value,Fields!Salesperson.Value)*100,0,System.MidpointRounding.AwayFromZero) >=5,"Green"
,"Yellow"))
When I run the report the background color expression only ever returns yellow. As a test I pasted the background color expression in as the cell value and ran it again. Results in the image below
I get no build or run time errors so I'm not sure why this does not work.
After some more searching I found a better Custom Code solution than what I was using to get the Growth Percentage in a column group compared to the previous column value. Besides being simpler to read this version has an added benefit: You can dynamically hide the growth percentage column for your first instance of the column group (because it will always be zero or null) and still get the right values in the 2nd/3rd/4th instance of the column group.
Public Function GetDeltaPercentage(ByVal PreviousValue, ByVal CurrentValue) As Object
If IsNothing(PreviousValue) OR IsNothing(CurrentValue) Then
Return Nothing
Else if PreviousValue = 0 OR CurrentValue = 0 Then
Return Nothing
Else
Return (CurrentValue - PreviousValue) / PreviousValue
End If
End Function
The new function is called like so
=Code.GetDeltaPercentage(Previous(Sum(<expression or dataset field>),"Group ByColumn"), Sum(<expression or dataset field>))
Re: the original question - why does my cell value expression not work when used as the background color expression - I took an easy out and just referenced the cell value.
=IIF(ROUND(Me.Value*100,0,System.MidpointRounding.AwayFromZero)<=-5,"Red"
,IIF(ROUND(Me.Value*100,0,System.MidpointRounding.AwayFromZero) >=5,"Green"
,"Yellow"))

Check if textfield text is almost equal to string

I have a UITextField called textfield. And I have this code to check if the text in the textfield is equal to "exampletext"
if ([textfield.text isEqualToString:#"exampletext"]) {
NSLog(#"Correct");
} else {
NSLog(#"Wrong");
}
But I also want to check if the text in the textfield is almost equal to "exampletext", if the text is almost the same as "exampletext". Like if the text was "eampletex" I want to NSLog(#"Close")
Are there any ways to check if the textfield text is like 50% equal to "exampletext"?
Or any ways to check if the textfield text has 50% the same characters as "exampletext"?
Or something else like that?
What you are looking for is an implementation of the levenshtein distance, levenshtein("hello", "hallo") => 1, levenshtein("hello", "ellos") => 2. You can check this library.
Once you have the distance between the two strings, you could get it as a percentage calculating: percentage = 100 * levenshtein(original,other) / length(original)
Here's my go at it. Create a custom character set from the string you want to match. Check each character in the texfield.text against that character set, and if the number of matches is close to the number of letters in the string, do something..
NSString *testString = #"wordToCompare";
NSString *textFromTextfield = textfield.text;
//create a custom character set from the word you want to compare to...
NSCharacterSet *characterSetForString = [NSCharacterSet characterSetWithCharactersInString:testString];
//keep track of how many matches...
int numberOfCharsThatMatchSet = 0;
for (int x = 0; x < [textFromTextField length]; x++) {
unichar charToCheck = [textFromTextField characterAtIndex:x];
if ([characterSetForString characterIsMember:charToCheck] == YES) {
numberOfCharsThatMatchSet++;
}
NSLog(#"%d", numberOfCharsThatMatchSet);
}
// if the number of matches is the same as the length of the word + or - 2...
if ((numberOfCharsThatMatchSet > [testString length] - 2 ) && (numberOfCharsThatMatchSet < [testString length] + 2 )) {
NSLog(#"close match...");
}
Not sure if this is 100% what you're looking for, but maybe it will help anyway...
I'm sure there might be some open source out there somewhere that would do this for you..however, one approach I can think of that will give you a bit of a lead...
Sort out the characters of both your strings into arrays. Determine which string you want to be the master string and grab the string length of it.
Now compare each character. Ex: Word 1: hello, Word 2: ello.
Each time a letter is found add one to a count. If by the end of your looping your count is 80% of the original length you grabbed from the master string or greater then you most likely have a partial match.
So for our example Word 1 will be our master string and its length is 5. "ello" contains 4/5 characters and therefore is matches 80% of the original string.
I don't think there is an easy way (with several lines of code) of solving this. There are several algorithms you might consider and pick the one which suits your needs most.
You should look at this question. Although it has been designed and answered for another language, you asked for a way or method so you have your solution there.

Drawing one NSString in multiple rects

I have a really long string, with thousands of line with a default font. So rather than draw the whole thing out in one table view cell, I'm going to make several cells to draw the same string, each drawing the next part of the string.
I'm having a hard time finding a starting point. Say I draw the first 500 pixels of height of the string in the rect - how do I know where to start in my second rect? If it's the same string, how can I specify for it to draw only some parts of the string?
Each cell will know it's own row number, so I'll be able to determine where exactly in the table I am, I just don't know how the string will know which part it's supposed to draw..
Or another question would be: how can I break up one string into multiple strings based on a certain number of lines?
Edit: Here are some NSString methods I'm finding that might be useful, but I still don't know how I would use them in my case:
- (void)getLineStart:(NSUInteger *)startIndex end:(NSUInteger *)lineEndIndex contentsEnd:(NSUInteger *)contentsEndIndex forRange:(NSRange)aRange
- (NSRange)lineRangeForRange:(NSRange)aRange
Use substringWithRange: This will allow you to select a start and end point of the string. I would grab each section by a number of characters. So section 1 would be 0-500 section 2 would be 500-1000. The problem here would be you may cut off in the middle of a sentence. You can use something like lineRangeForRange to determine your ranges for the substring.
lineRangeForRange
Returns the range of characters representing the line or lines containing a given range.
- (NSRange)lineRangeForRange:(NSRange)aRange
Parameters
aRange
A range within the receiver.
Return Value
The range of characters representing the line or lines containing aRange, including the line termination characters.
EDIT
NSString *string = #"tjykluytjghklukytgjhkkghkj sdkjlhfkjsadgfiulgeje fuaeyfkjasdgfueghf aksjgflkj. wyruehskjluishfoeifh uasyeajhkfa uiyelkjahsdf uayekljshdf aehkfjsd. \n I iheio;fajkdsf sdfhlueshkfjskdhf ujhelkjfh. luehljkfhlsdf. leufhlkjdshfa. \n euoiywhfldsjkhf euyhfsdlkj. ewhlkjfsd. euilhfsdkjishdkjf euhjklsfd. \n";
NSLog(#"string length:%i", [string length]);
NSRange range;
range.length = [string length]/2;
range.location = 0;
NSLog(#"LineRangeForRange:%i", [string lineRangeForRange:range].length);
NSLog(#"Substring:%#", [string substringWithRange:[string lineRangeForRange:range]]);
Log displays:
string length:295
LineRangeForRange:148
Substring:tjykluytjghklukytgjhkkghkj sdkjlhfkjsadgfiulgeje fuaeyfkjasdgfueghf aksjgflkj. wyruehskjluishfoeifh uasyeajhkfa uiyelkjahsdf uayekljshdf aehkfjsd.
So what this did was I supplied LineRangeForRange a range which was from zero to half of the string. It could the last end line "\n" with in that range. Then I grabbed that substring
Doesn't sound like a way I would wanna read text in an app. Why not use a textView?
Edit: my recommendation wasn't to put a textView in a tableViewCell but to display the beginning of the text truncated and on click push a viewControllers that displays a textView. Kind of like Mail does.

Objective C - Adding characters at specific parts of a string

I have made a quadratic equation solver for the iPhone and when the text box is clicked, my own custom keypad appears. I have a button that changes whether the number is positive or negative. Right now I what happens is that when the button is pressed (0 - current value of text) is what is displayed in the text box, so if the number is positive, it will become negative and if it is negative it will become positive. I am having some problems doing this so what I wanted to is to put a minus sign at the beginning of the string if the number is positive and if the number is negative, the minus sign will be removed. Can anyone give me guidance on this?
Instead of negating using a mathematical function I assigned a NSMutableString to my UITextField then I inserted a "-" sign using insertString:atIndex: then I reassigned the changed string to my UITextField. To toggle between positive and negative, I created an if function so if the float value of my textfield is greater or equal to 0, then an "-" is inserted but if the float value of my text field is less than zero, the "-" is removed using deleteCharactersInRange. Here is my code as it stands:
- (IBAction)positivity{
NSMutableString *a = [NSMutableString stringWithString:aVal.text];
if([aVal.text floatValue]>=0){
[a insertString: #"-" atIndex: 0];
aVal.text = a;
}
else if([aVal.text floatValue]<0){
NSRange range = {0,1};
[a deleteCharactersInRange:range];
aVal.text = a;
}
}
aVal is the name of the UITextField that i am changing.
An alternative to the straight string approach is to not use a string. A while back I wrote a graphing calculator for iPhone that stored the equation internally in an NSMutableArray of NSStrings. Each slot in the array corresponded to one element in the equation, such as "x", "^", "sin(", etc.
When I needed to negate the equation, it was much easier to tell the array to insertObject:#"-" atIndex:0 than to try and insert it directly into the string. Then whenever the array was changed, I just remade the equation string like this:
NSString * newEquation = [equationElements componentsJoinedByString:#""];
While you could directly manipulate a string representation of a numeric value, such an approach is a bad idea. Not only is it less efficient than other alternatives, but potentially incorrect. (For example, #Ken's answer would result in two minus signs.)
What you probably want to do is negate the numeric value (just multiply it by -1, or subtract it from 0 as you suggested) and reflect that change in the interface (you mention a text box).
If you're using standard Cocoa controls (which inherit from NSControl, as NSTextField does) I suggest using -[NSControl setIntegerValue:] to change the text of the text field. If you (can) break up your UI well and have a text field for each variable in the quadratic equation, this should be fairly simple. (If you're using something other than an integer value, use something like -setDoubleValue: or -setFloatValue: instead.)
If you must create your own string beforehand, using an integer format specifier will display a "-" sign automatically if appropriate. Be sure to use %ld instead of %d (thanks, #Peter!) as the format specifier for NSInteger values to avoid possibly truncating values larger than 32-bit. For example:
NSString *result = [NSString stringWithFormat:#"%ld", nsIntegerValue];
In a more general sense, if you need to insert a dynamically-obtained string (not just something for which you can create a format string at compile time) you can also use an NSMutableString and its methods -appendString: and -insertString:atIndex: as well.