nsrange

UITextView firstRectForRange not working when there's new line characters in the mix

半腔热情 提交于 2019-12-04 01:50:37
问题 I'm using this method for converting a NSRange to a CGRect as it relates to a UITextView: - (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView { UITextPosition *beginning = textView.beginningOfDocument; UITextPosition *start = [textView positionFromPosition:beginning offset:range.location]; UITextPosition *end = [textView positionFromPosition:beginning offset:range.location+range.length]; UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

Objective C: NSRange or similar with float?

∥☆過路亽.° 提交于 2019-12-04 01:36:37
问题 For some methods I want them to return a range of values (from: 235, to: 245). I did this using NSRange as a return value - (NSRange)giveMeARangeForMyParameter:(NSString *)myParameter; This works fine as long as the return value is an integer range (e.g. location: 235, length: 10). But now I have the problem that I need to return a float range (e.g. location: 500.5, length: 0.4). I read in the doc that NSRange is a typedefed struct with NSUIntegers . Is it possible to typedef another struct

Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range<Index>'(aka 'Range<String.CharacterView.Index>')

孤人 提交于 2019-12-04 00:21:44
Getting this error when checking the range for string characters... @objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { let shouldChange = false let text = textField.text var newString = text!.stringByReplacingCharactersInRange(range, withString: string) as? NSString if newString.length > 14{ newString = newString.substringToIndex(14) } textField.text = newString.uppercaseString return shouldChange } Instead of text! say (text! as NSString) . var newString = (text! as NSString).stringByReplacingCharactersInRange

Shortcut to generate an NSRange for entire length of NSString?

随声附和 提交于 2019-12-03 10:27:19
问题 Is there a short way to say "entire string" rather than typing out: NSMakeRange(0, myString.length)] It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire string)… [myString replaceOccurrencesOfString:@"replace_me" withString:replacementString options:NSCaseInsensitiveSearch range:NSMakeRange(0, myString.length)]; 回答1: Function? Category method? - (NSRange)fullRange { return (NSRange){0, [self length]}; }

NSAttributedString and emojis: issue with positions and lengths

人盡茶涼 提交于 2019-12-03 05:09:49
问题 I'm coloring some parts of a text coming from an API (think "@mention" as on Twitter) using NSAttributedString. The API gives me the text and an array of entities representing the parts of the text that are mentions (or links, tags, etc) which should be colored. But sometimes, the coloration is offset because of emojis. For example, with this text: "@ericd Some text. @apero" the API gives: [ { "text" : "ericd", "len" : 6, "pos" : 0 }, { "text" : "apero", "len" : 6, "pos" : 18 } ] which I

Shortcut to generate an NSRange for entire length of NSString?

穿精又带淫゛_ 提交于 2019-12-03 00:54:34
Is there a short way to say "entire string" rather than typing out: NSMakeRange(0, myString.length)] It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire string)… [myString replaceOccurrencesOfString:@"replace_me" withString:replacementString options:NSCaseInsensitiveSearch range:NSMakeRange(0, myString.length)]; Function? Category method? - (NSRange)fullRange { return (NSRange){0, [self length]}; } [myString replaceOccurrencesOfString:@"replace_me" withString:replacementString options:NSCaseInsensitiveSearch

How to get NSRange(s) for a substring in NSString? [duplicate]

陌路散爱 提交于 2019-12-03 00:49:19
This question already has answers here : How to get all NSRange of a particular character in a NSString? (4 answers) NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot..."; How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string. NSRange range = NSMakeRange(0, _stringLength); while(range.location != NSNotFound) { range = [[attString string] rangeOfString: @"California" options:0 range:range];

ios nsrange char from end

核能气质少年 提交于 2019-12-02 08:49:23
问题 Let's say I have: this - is an - example - with some - dashes NSRange will pick up the first instance of "-" with `rangeOfString:@"-" but what if I only want the last one? I'm looking for something that works similar to lastIndexOf in JS . Thanks! 回答1: One of the options you can pass to [NSString rangeOfString: options:] is NSBackwardsSearch (which would give you the last item in the string you're looking at). 回答2: There's another variant of the rangeOfString method: - (NSRange)rangeOfString:

ios nsrange char from end

ⅰ亾dé卋堺 提交于 2019-12-02 06:12:49
Let's say I have: this - is an - example - with some - dashes NSRange will pick up the first instance of "-" with `rangeOfString:@"-" but what if I only want the last one? I'm looking for something that works similar to lastIndexOf in JS . Thanks! One of the options you can pass to [NSString rangeOfString: options:] is NSBackwardsSearch (which would give you the last item in the string you're looking at). There's another variant of the rangeOfString method: - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange This lets you specify a range to

How do I use NSRange with this NSString?

落花浮王杯 提交于 2019-12-02 06:12:39
问题 I have the following NSString : productID = @"com.sortitapps.themes.pink.book"; At the end, "book" can be anything.... "music", "movies", "games", etc. I need to find the third period after the word pink so I can replace that last "book" word with something else. How do I do this with NSRange? Basically I need this: partialID = @"com.sortitapps.themes.pink."; 回答1: You can try a backward search for the dot and use the result to get the desired range: NSString *str = @"com.sortitapps.themes