草庐IT

ios - 来自 Swift Range 的 NSRange?

问题:NSAttributedString在我使用使用Range的SwiftString时采用NSRangelettext="Longparagraphsayingsomethinggoeshere!"lettextRange=text.startIndex..()inif(substring=="saying"){attributedString.addAttribute(NSForegroundColorAttributeName,value:NSColor.redColor(),range:substringRange)}})产生以下错误:error:'Range'isnotco

ios - 来自 Swift Range 的 NSRange?

问题:NSAttributedString在我使用使用Range的SwiftString时采用NSRangelettext="Longparagraphsayingsomethinggoeshere!"lettextRange=text.startIndex..()inif(substring=="saying"){attributedString.addAttribute(NSForegroundColorAttributeName,value:NSColor.redColor(),range:substringRange)}})产生以下错误:error:'Range'isnotco

nsstring - NSRange 到 Range<String.Index>

如何转换NSRange至Range在swift?我想使用以下UITextFieldDelegate方法:functextField(textField:UITextField!,shouldChangeCharactersInRangerange:NSRange,replacementStringstring:String!)->Bool{textField.text.stringByReplacingCharactersInRange(???,withString:string) 最佳答案 从Swift4(Xcode9)开始,Sw

nsstring - NSRange 到 Range<String.Index>

如何转换NSRange至Range在swift?我想使用以下UITextFieldDelegate方法:functextField(textField:UITextField!,shouldChangeCharactersInRangerange:NSRange,replacementStringstring:String!)->Bool{textField.text.stringByReplacingCharactersInRange(???,withString:string) 最佳答案 从Swift4(Xcode9)开始,Sw

ios - 在 UITextView 中滚动后获取可见文本的 NSRange

我正在尝试将滚动文本的位置保存在UITextView中,以便我可以在再次加载ViewController时返回到该位置。我有很长的字符串,所以我希望用户能够滚动到特定位置,然后稍后返回到该位置。我正在使用UITextView。scrollRangeToVisible函数自动滚动TextView,但我不知道如何获取用户看到的文本的NSRange。这是最好的方法吗?我尝试使用setContentOffset函数,但似乎没有任何效果。感谢任何帮助。谢谢! 最佳答案 这里是UITextView的一个小扩展,它使用了它的characterRa

ios - 如何比较不区分大小写的 NSRange ?

我写了一个在地址簿中搜索人的方法,我希望即使调用方法[someAdressBooksearchName:@"joh"];也能找到“johnbigs”;.我的方法适用于全名,但我在部分名称中遇到问题,这是我的代码:-(NSMutableArray*)searchName:(NSString*)someName{NSRangerange;NSMutableArray*results=[[NSMutableArrayalloc]init];for(AddressCards*addressCardinbook){if(someName!=nil&&[someNamecaseInsensitiv

ios - NSRange,在段落中找到一个词得到错误的结果

我编写了一种方法,通过向它发送该词的NSString来突出显示段落中的词,在我遇到这种情况之前,它一直运行良好:当我有这段文字时:Theirmotherhastrieddressingtheminother...当我传递单词other时,单词“mother”被突出显示,当我传递in时也是如此我得到了“dressing”。这是我的代码:-(void)setTextHighlited:(NSString*)txt{NSMutableAttributedString*string=[[NSMutableAttributedStringalloc]initWithString:self.tex

ios - 对包含 NSRange 值的数组进行排序

有一个包含一些NSRange值的数组:@[[NSValevalueWithRange:{5,3}],[NSValevalueWithRange:{2,3}],[NSValevalueWithRange:{7,3}],[NSValevalueWithRange:{1,3}],....]每个range都有不同的location,我希望数组按range.location倒序排序,有什么好的解决方案吗? 最佳答案 sortedArrayUsingComparator:是您任务的最佳选择。您的案例代码是:NSArray*array=@[[NS

ios - 如何将 NSRange 与此 NSString 一起使用?

我有以下NSString:productID=@"com.sortitapps.themes.pink.book";最后,“书”可以是任何东西……“音乐”、“电影”、“游戏”等。我需要找到pink之后的第三个句点,这样我就可以用其他东西替换最后一个“book”这个词。我如何使用NSRange执行此操作?基本上我需要这个:partialID=@"com.sortitapps.themes.pink."; 最佳答案 您可以尝试向后搜索点并使用结果来获得所需的范围:NSString*str=@"com.sortitapps.themes.

ios - NSRange `replaceCharactersInRange` 在退格和输入新字符后应用程序崩溃

我有一个文本字段,并使用shouldChangeCharactersInRange方法来获取值。这是我的代码:-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{NSLog(@"Pressing:%@\n",string);NSMutableString*text=[NSMutableStringstringWithString:textField.text];NSLog(@"length:%lu",