nsrange

Detect Character Tapped in UITextView

时光毁灭记忆、已成空白 提交于 2019-12-18 18:07:50
问题 I'm using the below code to detect words tapped in a UITextView. This works fine, but I want to detect some special characters, like a ? . ? doesn't show up as part of a word when using UITextGranularityWord and I can't seem to get it to show up when using UITextGranularityCharacter either. How can I detect taps on single special characters such as the ? ? -(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv { //eliminate scroll offset pos.y += _tv.contentOffset.y; //get

What's the best way to cut Swift string into 2-letter-strings?

元气小坏坏 提交于 2019-12-18 09:32:29
问题 I need to split a string into 2-letter pieces. Like “friend" -> "fr" "ie" "nd". (Okay, its a step for me to change HEX string to Uint8 Array) My code is for i=0; i<chars.count/2; i++ { let str = input[input.startIndex.advancedBy(i*2)..<input.startIndex.advancedBy(i*2+1)] bytes.append(UInt8(str,radix: 16)!) } But I don't know why I cannot use Range to do this split. And I have no idea what will happen when i*2+1 is bigger than string's length. So what's the best way to cut Swift string into 2

NSRange to Range<String.Index>

房东的猫 提交于 2019-12-16 19:54:53
问题 How can I convert NSRange to Range<String.Index> in Swift? I want to use the following UITextFieldDelegate method: func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool { textField.text.stringByReplacingCharactersInRange(???, withString: string) 回答1: The NSString version (as opposed to Swift String) of replacingCharacters(in: NSRange, with: NSString) accepts an NSRange , so one simple solution is to convert String to

Why does setting an attributedText's background raise an NSRangeException error?

China☆狼群 提交于 2019-12-13 21:57:25
问题 So I'm trying to set a blue background color to a UILabel 's attributed text dynamically as the user pans their finger along the screen but I can't for the life of me figure out why I'm getting an NSRangeException with the following error message: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds at times. Here's my code: var afterContext = "" if textDocumentProxy.documentContextAfterInput != nil {

Get current paragraph in UITextView?

女生的网名这么多〃 提交于 2019-12-12 21:13:32
问题 I want to detect the current paragraph, this is my code so far, but it doesn't work so well. Lets say I have 3 paragraphs and when the cursor is between them, it selects the next one, which is not right. Is there a better way to do this? With this code, I want to detect the current paragraph, then change the font of that paragraph, and then continue writing with that font. func textViewDidChangeSelection(textView: UITextView) { // print("selected") //stylesDefaults() var arr = [String]()

Get the NSRange for the visible text after scroll in UITextView

╄→гoц情女王★ 提交于 2019-12-12 15:12:42
问题 I'm trying to save the location of scrolled text in a UITextView so that I can return to that location upon loading the ViewController again. I have very long strings, so I want the user to be able to scroll to a specific location and then return to that location later. I'm using the UITextView . scrollRangeToVisible function to automatically scroll the text view, but I don't know how to get the NSRange of the text that the user is seeing. Is this the best way to go about this? I tried using

Treat a single integer value as a range in Swift

泪湿孤枕 提交于 2019-12-12 09:53:17
问题 I need to validate the length of a string. The allowed values for the character count are: 6 – 9 characters 12 characters 15 characters All strings with a different character count are invalid. Thus, I would like to create a Swift function that accepts a number of ranges and evaluates the string: extension String { func evaluateLength(validCharacterCounts: Range<Int>...) -> Bool { // Implementation } } Now I can call the function for a single Int range: "Live long and prosper".evaluateLength

NSRangeException: Call Stack Not Showing Line Number

本小妞迷上赌 提交于 2019-12-12 07:58:09
问题 I am getting the following index out of bounds error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x2263052 0x24c7d0a 0x224fdb8 0x2f4a7 0x2264ec9 0x81e299 0x81e306 0x75aa30 0x75ac56 0x741384 0x734aa9 0x39a9fa9 0x22371c5 0x219c022 0x219a90a 0x2199db4 0x2199ccb 0x39a8879 0x39a893e 0x732a9b 0x1e5b 0x1dc5 0x1) I know exactly what the error means, but I find these

Getting index of a character in NSString with offset & using substring in Objective-C

爷,独闯天下 提交于 2019-12-12 07:41:17
问题 I have a string! NSString *myString=[NSString stringWithFormat:@"This is my lovely string"]; What I want to do is: Assuming the first character in the string is at index 0. Go to the 11th character (That is 'l' in the above case), and find the position of first occurring space backwards (In the above string, the position of first occurring space if we go backwards from 'l' is at position 10). Let's call the index of this space 'leSpace' having value 10. Substring the remaining string to a new

Why is my NSRange always 0?

眉间皱痕 提交于 2019-12-11 18:33:10
问题 I want to implement a delete key for my calculator app. My pseudocode for this was: foo = length of current number bar = length of current number-1 current number = current number with character at index (foo-bar) removed To do this in objective-c I have tried to implement this using the NSRange function as well as the StringbyappendingString method: - (IBAction)DelPressed { if ([self.display.text length] >=2) { int len = [self.display.text length];//Length of entire display //Add to the