How to find caret position in an NSTextView? - objective-c

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

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:.

How to make a NSString always use fixed font width

I want to have an NSString in the format "hh:mm:ss" to use exactly the same amount of pixel space as the string "88:88:88". Is that possible?
Now I'm using:
// ...
NSMutableString * strS = [[NSMutableString alloc] initWithFormat:#"%d", Seconds];
if (Seconds<10){
[strS insertString:#"0" atIndex:0];
}
// Make the time to show
[ClocklLabel setText:[NSString stringWithFormat:#"%2#:%2#:%2#", strH,strM,strS]];
with no success!
I think you're looking for a monospaced font. Try using Courier for example:
UIFont *courier = [UIFont fontWithName:#"Courier" size:12.0f];
label.font = courier;
If you're trying to create a digital clock display, you could also simply use a separate label for each number and position the labels however you want.
You could use a monospace font. If you do not want this, use a UILabel and set adjustsFontSizeToFitWidth to YES. However, in this case the height will vary.
The title of your question hints to the fact that you are missing an important detail. NSString only contains the actual text.
It is not the NSString that dictates font, color or other attributes. As other answers have suggested you should set the font to the label/textfield/button or whatever visual gadget is supposed to display the string.
If you want to combine both text and text attributes, you may want to check NSAttributedString.

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.

How do I display the character for 1/2 in an NSString in iOS?

I'm working on an application for iOS in which I would like to place the fractional value 1/2 as a single character within an NSString for use in an UILabel, is this possible?
Try setting the label's text property to the Unicode of 1/2:
label.text = [NSString stringWithFormat:#"%C",0x00bd];
You can simply use Mac OS's built-in character viewer: http://docs.info.apple.com/article.html?path=Mac/10.6/en/8164.html
So you can just do label.text = #"½"

Extract values between parenthesis from NSString

I have an NSString that will be something like "xxxx (yyyyy)" where x and y can be any character. I'd like to extract just the y from inside the parenthesis. I managed to extract the x using a NSScanner but I haven't figured out the proper way to extract out the y.
Just to be complete:
If you are absolutely sure of the format of your output you can use the array methods:
NSString *inputString; // this is in the form "xxxx(yyyy)"
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:#"()"];
NSArray *splitString = [inputString componentsSeparatedByCharactersInSet:delimiters];
NSString *xString = [splitString objectAtIndex:0];
NSString *yString = [splitString objectAtIndex:1];
Of course, you need to be sure that the delimiting characters don’t exist in the inputString
Easiest way would be to use RegExKit:
http://regexkit.sourceforge.net/
Then you'd do something like:
[#"xxxx(yyyyy)" getCapturesWithRegexAndReferences:#"\\((.*)\\)",#"$1", &extractedString,nil];
and extractedString would contain whatever was in parenthesis.
Scan up to the ‘(’, then scan it, then scan up to the ')'. The result of the last scan is yyyy.
you might have a look at PKTokenizer in ParseKit:
http://parsekit.com