Getting Remaining Text After Selection is Cut - ios

Is there a simple way of getting remaining text after selection is cut? Let me suppose that I have a textview control with "Hello, Jim" in it. If "Jim" is selected, I want to store "Hello" to a variable, " without using [textview1 cut:self]. In the following code, I can get the other part.
UITextRange *selectedTextRange = textview1.selectedTextRange;
NSUInteger location = [textview1 offsetFromPosition:textview1.beginningOfDocument toPosition:selectedTextRange.start];
NSUInteger length = [textview1 offsetFromPosition:selectedTextRange.start toPosition:selectedTextRange.end];
NSRange selectedRange = NSMakeRange(location, length);
NSString *str = [textview1.text substringWithRange:selectedRange];
NSLog(#"%#",str);
Of course, that's the other part.
Thank you for your help.

NSString *remaining = [textview1.text stringByReplacingCharactersInRange:textview1.selectedRange withString:#""];

I think it's something like the following. (Revised)
NSMutableString *str = [NSMutableString stringWithString:#""];
if (textview1.text.length > length) {
[str appendString:[textview1.text substringToIndex:location]];
[str appendString:[textview1.text substringFromIndex:location + length]];
} else {
[str appendString:#""];
}
[self setText:str];

Related

NSString search for character from the location of the cursor in a UITextView

I am stuck with trying to figure out how I can find the occurrence of a character from a specified range (i.e. the current position of the cursor).
I know how to find the current position of the cursor using
NSRange cursorPosition = [textView.text selectedRange];
But I am trying to figure out how I can search backward from cursorPosition.
What I am trying to do is, for example, if I have a string:
NSString *string = #"Hello I am tagging #xyz to notify them of the tag"
Suppose in string, the location of the cursor is just before "to", I want to search from the location of the cursor to the location of # in the string and take the substring from the cursorPosition to #.
Please let me know if my description is vague or not well written, I will explain it further.
Any help would be great! Thanks a lot!
Edit: Thanks a lot for all your time and responses!
Simply use rangeOfString:options:range:.
// Get the current selection range
NSRange cursorPosition = [textView.text selectedRange];
if (cursorPosition.location != NSNotFound) {
// Build range from start of text up to the start of the selection
NSRange searchRange = NSRangeMake(0, cursorPosition.location);
// Find the desired substring within the range
NSRange matchRange = [textView.text rangeOfString:#"#" options:NSBackwardsSearch range:searchRange];
if (matchRange.location != NSNotFound) {
// Build range starting with the found substring up to the start of the selection
NSRange textRange = NSRangeMake(matchRange.location, searchRange.length - matchRange.location);
// Get the text in the desired range
NSString *matchingText = [textView.text substringInRange:textRange];
NSLog(#"Matching text to caret is %#", matchingText);
} else {
NSLog(#"No match up to the caret");
}
} else {
NSLog(#"No selection");
}
Note that this code looks up to the start of any current selection in the text view. If you want to search within the current selection you will need to adjust accordingly.
Try this
NSRange cursorPosition = [textView.text selectedRange];
NSString *string = #"Hello I am tagging #xyz to notify them of the tag";
NSInteger loc = [string rangeOfString : #"#"].location;
NSRange range = NSMakeRange(loc, (cursorPosition.location - loc));
NSString *newString = [string substringWithRange: range];
NSInteger cursorPosition = [_textView selectedRange].location;
NSInteger stringPosition = [_textView.text rangeOfString:#"#"].location;
if (stringPosition < cursorPosition) {
stringPosition = stringPosition + 1;
}
NSInteger lengthFinal = labs(cursorPosition - stringPosition);
NSRange range = NSMakeRange(MIN(cursorPosition, stringPosition), lengthFinal);
NSString *finalSubString = [_textView.text substringWithRange:range];
It is working for me. Place cursor and enter back button to test
-(BOOL)textView:(nonnull UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(nonnull NSString *)text{
NSRange rangeFrom = [textView.text rangeOfString:#"#"];
NSString *result = [textView.text substringWithRange:NSMakeRange(rangeFrom.location+1, range.location-rangeFrom.location)];
NSLog(#"result:%#",result);//result:xyz
return YES;
}
// ...........************ check with this....Why not is this working....*/
NSRange cursorPosition = NSMakeRange(23, 1);//[textView.text range];//selectedRange
NSString *string = #"Hello I am tagging #xyz to notify them of the tag";
NSRange rangeFrom = [string rangeOfString:#"#"];
NSString *result = [string substringWithRange:NSMakeRange(rangeFrom.location+1, cursorPosition.location-rangeFrom.location)];
NSLog(#"result:%#",result);//result:xyz

In objective-c how to get characters after n-th?

I have a number which will be represented as string. It is longer than 4 chars. I need to create new string from 5th till the end for that number.
For example if I have 56789623, I need to have 9623 as a result (5678 | 9623).
How to do that?
P.S. I suppose that this is very simple question, but I don't know how properly ask Google about that.
NSString *str = #"56789623";
NSString *first, *second;
if ([str length] > 4) {
first = [str substringWithRange:NSMakeRange(0, 4)];
second = [str substringWithRange:NSMakeRange(4, [str length] - 4)];
} else {
first = str;
second = nil;
}
Use this Simple functions
- (NSString *)substringFromIndex:(NSUInteger)from;
- (NSString *)substringToIndex:(NSUInteger)to;
- (NSString *)substringWithRange:(NSRange)range;
You can use:
- (NSString *)substringFromIndex:(NSUInteger)anIndex
NSString *number = #"56789623";
NSString *result = [number substringFromIndex:4];
NSLog(#"%#", result);
result contains the string: #"9623"
The keywords you were looking for are: substring and range. There are several ways to use them. Example code split string into 2 equal (if number of characters is even almost equal) substrings:
NSString *str = #"56789623";
NSInteger middleIndex = (NSInteger)(str.length/2);
NSString *strFirstPart = [str substringToIndex:middleIndex];
NSString *strSecondPart = [str substringFromIndex:middleIndex];
NSString *strFirstPart2 = [str substringWithRange:NSMakeRange(0, middleIndex)];
NSString *strSecondPart2 = [str substringWithRange:NSMakeRange(middleIndex, [str length]-middleIndex)];

Backward with custom string

I used a string array for emoticons like this:
NSArray *emoticons = #[#"[smile]",#"[cry]",#"[happy]" ...]
then in a UITextView displaying a string like this:
I'm so happy now [happy] now [smile]
When I click a backward or delete button, if the last word is in emoticons, I want a whole emoticon string be deleted, not the last one character only.
Any idea?
Try this,
NSString *string = self.textView.text;
__block NSString *deleteWord = nil;
__block NSRange rangeOfWord;
[string enumerateSubstringsInRange:NSMakeRange(0, self.textView.selectedRange.location + self.textView.selectedRange.length) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
deleteWord = substring;
rangeOfWord = enclosingRange;
*stop = YES;
}];
if ([emoticons containsObject:deleteWord]) {
string = [string stringByReplacingCharactersInRange:rangeOfWord withString:#""];
self.textView.text = string;
self.textView.selectedRange = NSMakeRange(rangeOfWord.location, 0);
}
You might achieve something like this with the UITextViewDelegate method textView:shouldChangeTextInRange:replacementText: checking what is about to be deleted and remove the whole [emoticon] word.
I am giving you the idea that i used.
as you do not mentioned what you used as emoticons.
but for delete logic i think you will get idea from my this code.
if ([string isEqualToString:#""]) {
NSString *lastChar = [txthiddenTextField.text substringFromIndex: [txthiddenTextField.text length] - 1];
NSLog(#"Last char:%#",lastChar);
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
NSString *strPlaceHolder;
strPlaceHolder = txthiddenTextField.text;
if([lastChar isEqualToString:#"]"])
{
int j = 1;
for (int i = [txthiddenTextField.text length]-1; i >=0; --i)
{
NSString *lastChar = [txthiddenTextField.text substringFromIndex: [txthiddenTextField.text length] - 1];
if([lastChar isEqualToString:#"["])
{
NSLog(#"%d",j);
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
// NSLog(#"Processing character %#",strPlaceHolder);
break;
}
txthiddenTextField.text = [txthiddenTextField.text substringToIndex:[txthiddenTextField.text length] - 1];
j = j+1;
}
}
NSLog(#"My text fild value :%#",txthiddenTextField.text);
return YES;
}
So, from here you have to check if the closing bracket is coming or not.
if closing bracket will come then up to opening bracket you have to delete.
then whole emoticon will delete.
hope this helps....

iOS: changing NSString value

Will this bit of code produce any memory leaks? Is it the correct way to change NSString values?
NSString * enemiesAndElementsTextureFileName = #"bla bla";
enemiesAndElementsTextureFileName = #"bl";
That way of doing it won't cause any memory leaks and it is indeed correct. In this case you wouldn't need an NSMutableString because you aren't altering the string literal itself, you are simply replacing the string value with a new one (replacing #"bla bla" with #"bl").
In this case, however, your string will now be 'bl', so you can delete that first line value and just have NSString * enemiesAndElementsTextureFileName = #"bl";
Yes NSString allocated once. This is one of the way
Yes, use NSMutableString with the following method as your needs:
// Allocate
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
// set string content
[str setString:#"1234"];
// Append
[str appendString:#"567"];
// Concat
[str appendFormat:#"age is %i and height is %.2f", 27, 1.55f];
// Replace
NSRange range = [str rangeOfString:#"height"];//查找字符串height的位置
[str replaceCharactersInRange:range withString:#"no"];
// Insert
[str insertString:#"abc" atIndex:2];
// Delete
range = [str rangeOfString:#"age"];
[str deleteCharactersInRange:range];
NSLog(#"%#", str);

Truncate string containing emoji or unicode characters at word or character boundaries

How can I truncate a string at a given length without annihilating a unicode character that might be smack in the middle of my length? How can one determine the index of the beginning of a unicode character in a string so that I can avoid creating ugly strings. The square with half of an A visible is the location of another emoji character which has been truncated.
-(NSMutableAttributedString*)constructStatusAttributedStringWithRange:(CFRange)range
NSString *original = [_postDictionay objectForKey:#"message"];
NSMutableString *truncated = [NSMutableString string];
NSArray *components = [original componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for(int x=0; x<[components count]; x++)
{
//If the truncated string is still shorter then the range desired. (leave space for ...)
if([truncated length]+[[components objectAtIndex:x] length]<range.length-3)
{
//Just checking if its the first word
if([truncated length]==0 && x==0)
{
//start off the string
[truncated appendString:[components objectAtIndex:0]];
}
else
{
//append a new word to the string
[truncated appendFormat:#" %#",[components objectAtIndex:x]];
}
}
else
{
x=[components count];
}
}
if([truncated length]==0 || [truncated length]< range.length-20)
{
truncated = [NSMutableString stringWithString:[original substringWithRange:NSMakeRange(range.location, range.length-3)]];
}
[truncated appendString:#"..."];
NSMutableAttributedString *statusString = [[NSMutableAttributedString alloc]initWithString:truncated];
[statusString addAttribute:(id)kCTFontAttributeName value:[StyleSingleton streamStatusFont] range:NSMakeRange(0, [statusString length])];
[statusString addAttribute:(id)kCTForegroundColorAttributeName value:(id)[StyleSingleton streamStatusColor].CGColor range:NSMakeRange(0, [statusString length])];
return statusString;
}
UPDATE Thanks to the answer, was able to use one simple function for my needs!
-(NSMutableAttributedString*)constructStatusAttributedStringWithRange:(CFRange)range
{
NSString *original = [_postDictionay objectForKey:#"message"];
NSMutableString *truncated = [NSMutableString stringWithString:[original substringWithRange:[original rangeOfComposedCharacterSequencesForRange:NSMakeRange(range.location, range.length-3)]]];
[truncated appendString:#"..."];
NSMutableAttributedString *statusString = [[NSMutableAttributedString alloc]initWithString:truncated];
[statusString addAttribute:(id)kCTFontAttributeName value:[StyleSingleton streamStatusFont] range:NSMakeRange(0, [statusString length])];
[statusString addAttribute:(id)kCTForegroundColorAttributeName value:(id)[StyleSingleton streamStatusColor].CGColor range:NSMakeRange(0, [statusString length])];
return statusString;
}
NSString has a method rangeOfComposedCharacterSequencesForRange that you can use to find the enclosing range in the string that contains only complete composed characters. For example
NSString *s = #"😄";
NSRange r = [s rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
gives the range { 0, 2 } because the Emoji character is stored as two UTF-16 characters (surrogate pair) in the string.
Remark: You could also check if you can simplify your first loop by using
enumerateSubstringsInRange:options:usingBlock
with the NSStringEnumerationByWords option.
"truncate a string at a given length" <-- Do you mean length as in byte length or length as in number of characters? If the latter, then a simple substringToIndex: will suffice (check the bounds first though). If the former, then I'm afraid you'll have to do something like:
NSString *TruncateString(NSString *original, NSUInteger maxBytesToRead, NSStringEncoding targetEncoding) {
NSMutableString *truncatedString = [NSMutableString string];
NSUInteger bytesRead = 0;
NSUInteger charIdx = 0;
while (bytesRead < maxBytesToRead && charIdx < [original length]) {
NSString *character = [original substringWithRange:NSMakeRange(charIdx++, 1)];
bytesRead += [character lengthOfBytesUsingEncoding:targetEncoding];
if (bytesRead <= maxBytesToRead)
[truncatedString appendString:character];
}
return truncatedString;
}
EDIT: Your code can be rewritten as follows:
NSString *original = [_postDictionay objectForKey:#"message"];
NSArray *characters = [[original componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF != ''"]];
NSArray *truncatedCharacters = [characters subarrayWithRange:range];
NSString *truncated = [NSString stringWithFormat:#"%#...", [truncatedCharacters componentsJoinedByString:#" "]];

Resources