nsrange

Do these two ranges intersect? If so, how do I know?

丶灬走出姿态 提交于 2019-12-11 09:10:07
问题 From Apple's documentation: Return Value A range describing the intersection of range1 and range2—that is, a range containing the indices that exist in both ranges. Discussion If the returned range’s length field is 0, then the two ranges don’t intersect, and the value of the location field is undefined. Okay, so let's say I have two ranges: (lldb) p rangeOne (NSRange) $3 = location=11, length=4 (lldb) p rangeTwo (NSRange) $4 = location=14, length=0 And I calculate the intersection: NSRange

NSMutableString modify while iterating text checking results

本秂侑毒 提交于 2019-12-11 09:01:06
问题 I'm trying to modify an NSString while iterating NSTextCheckingResults from an NSRegularExpression . I know that it won't work the way I implemented it as every replacement changes the length of the string an so the validity of the NSRages in my loop. How can I replace multiple matches in a for loop? Here is my code: NSMutableString *string = [@"[H]…[mm]…[s]" mutableCopy]; NSReguralExpression *exp = [NSRegularExpression regularExpressionWithPattern:@"(\\[[Hms]{1,2}\\])" options:0 error:nil];

Can NSRange determine if a snippet of text exists in a larger string?

佐手、 提交于 2019-12-11 02:27:10
问题 I have a large string coming back from an http GET and I'm trying to determine if it has a specific snippet of text or not (please forgive my sins here) My question is this: Can / Should I use NSRange to determine if this snippet of text does exist? NSRange textRange; textRange =[[responseString lowercaseString] rangeOfString:[@"hat" lowercaseString]]; if(textRange.location != NSNotFound) { //do something magical with this hat } Thank you in advance! 回答1: You can check to see if the location

Setting attributedText, NSRangeException error

心不动则不痛 提交于 2019-12-10 22:32:20
问题 Trying to set my UIText view's attributed text properties via selection. Almost works. Action to set text with red font color below. This works sometimes, but often gives an error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds' This happens even while there seems to be many more characters in the text view than indicated by the selected range. - (IBAction)setText:(id)sender { NSMutableAttributedString *

How to set up a tap gesture only for specific ranges of a UILabel in Swift 3

给你一囗甜甜゛ 提交于 2019-12-09 19:27:47
问题 I have an attributed string set to UILabel with multiple underlines , colors like below image and I know How to setup a tap gesture for whole label (with enabling user interaction ) and below is my code for what I have done including setting up underline and setting up font colors for multiple ranges . import UIKit class ViewController: UIViewController { @IBOutlet weak var mylabel: UILabel! var theString = "I have agree with the terms and conditions and privacy policy" override func

String, substring, Range, NSRange in Swift 4

こ雲淡風輕ζ 提交于 2019-12-09 02:30:02
问题 I am using the following code to get a String substring from an NSRange : func substring(with nsrange: NSRange) -> String? { guard let range = Range.init(nsrange) else { return nil } let start = UTF16Index(range.lowerBound) let end = UTF16Index(range.upperBound) return String(utf16[start..<end]) } (via: https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/) When I compile with Swift 4 (Xcode 9b4), I get the following errors for the two lines that declare start and end : 'init' is

NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds

梦想的初衷 提交于 2019-12-05 17:39:11
问题 I try to assign attributes to 3 last chars of newClock string, which is @"3:33:23" . However I get an error when construct NSRange : NSMutableAttributedString *mas = [[NSMutableAttributedString alloc]initWithString:newClock]; [mas addAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor], NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:12]} range:NSMakeRange(newClock.length-3,newClock.length)]; 回答1: NSMakeRange(i, j) creates a range with location i and length j.

How to create and use temp NSRange in lldb?

▼魔方 西西 提交于 2019-12-05 01:45:48
NSRange is just a C struct. I want to create a temporary one in lldb in Xcode at a breakpoint. Specifically for use in NSArray method objectAtIndex:inRange: This does not work. (lldb) expr NSRange $tmpRange = (NSRange){0,4} (lldb) expr $tmpRange (NSRange) $tmpRange = location=0, length=4 (lldb) expr -o -- [items indexOfObject:item4 inRange:$tmpRange] error: no matching constructor for initialization of 'NSRange' (aka '_NSRange') error: 1 errors parsing expression My code has an NSRange var named badRange at the breakpoint, and passing that one in works. Thus: (lldb) expr -o -- [items

How to set up a tap gesture only for specific ranges of a UILabel in Swift 3

假如想象 提交于 2019-12-04 16:32:24
I have an attributed string set to UILabel with multiple underlines , colors like below image and I know How to setup a tap gesture for whole label (with enabling user interaction ) and below is my code for what I have done including setting up underline and setting up font colors for multiple ranges . import UIKit class ViewController: UIViewController { @IBOutlet weak var mylabel: UILabel! var theString = "I have agree with the terms and conditions and privacy policy" override func viewDidLoad() { super.viewDidLoad() mylabel.text = theString let tap = UITapGestureRecognizer(target: self,

Using character delimiters to find and highlight text in Swift

天大地大妈咪最大 提交于 2019-12-04 12:49:45
I previously developed an android app that served as a reference guide to users. It used a sqlite database to store the information. The database stores UTF-8 text without formatting (i.e. bold or underlined) To highlight what sections of text required formatting I enclosed them using delimiter tokens specifically $$ as this does not appear in the database as information. Before displaying the text to the user I wrote a method to find these delimiters and add formatting to the text contained within them and delete the delimiters. so $$foo$$ became foo . My java code for this is as follows: