nsrange

encodedOffset deprecation

老子叫甜甜 提交于 2019-11-29 07:34:50
In my application I have some code to fetch the range of the host in a URL . It looks like this: private func rangeOfHost(text: String) -> NSRange? { let url = URL(string: text) if let host: String = url?.host { if let range = text.range(of: host) { return NSRange(location: range.lowerBound.encodedOffset, length: range.upperBound.encodedOffset - range.lowerBound.encodedOffset) } } return nil } Xcode has been warning me that 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use utf16Offset(in:) to achieve the same behavior. . However, it's not

Convert selectedTextRange UITextRange to NSRange

不打扰是莪最后的温柔 提交于 2019-11-28 22:26:47
问题 How can I convert a UITextRange object to an NSRange ? I've seen plenty of SO posts about going the other direction but that's the opposite of what I need. I'm using the UITextRange selectedTextRange which is a property of a UITextView . It returns a UITextRange but I need a range. 回答1: You need something like this: - (NSRange) selectedRangeInTextView:(UITextView*)textView { UITextPosition* beginning = textView.beginningOfDocument; UITextRange* selectedRange = textView.selectedTextRange;

How to capture last 4 characters from NSString

你。 提交于 2019-11-28 18:12:36
I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string . I have looked through NSString Class Reference library and the only real option I have found that looks like it will do what I want it to is - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange I have used this once before but with static parameters 'that do not change', But for this implementation I am wanting to use non static parameters that change depending on the size of the string coming in. So far

Make part of a UILabel bold in Swift

守給你的承諾、 提交于 2019-11-28 17:50:30
I have a UILabel I've made programmatically as: var label = UILabel() I've then declared some styling for the label, including a font, such as: label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30) label.font = UIFont(name: "Typo GeoSlab Regular Demo", size: 15) label.textColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 91/100, alpha: 1) The first part of the label will always read : "Filter:" then followed by another part of the string, for example, "Most popular" I would like the word filter to be in bold, so the whole thing would look like: Filter:

NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of bounds

感情迁移 提交于 2019-11-28 12:04:51
I'm trying to add some fancy text to a label, but I've run into some problems with the NSMutableAttributedString class. I was trying to achieve four this: 1. Change font, 2. Underline range, 3. Change range color, 4. Superscript range. This code: - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { NSMutableAttributedString* display = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"]; NSUInteger length = [[display string]length] - 1; NSRange wholeRange = NSMakeRange(0, length); NSRange helloRange = NSMakeRange(0, 4); NSRange worldRange = NSMakeRange(6, length

How to get all NSRange of a particular character in a NSString?

送分小仙女□ 提交于 2019-11-28 10:47:40
I have two NSStrings: orgText and searchLetter . I want to highlight every occurrences of the searchLetter in the orgText with a red color. How can I get the NSRange of all occurrences of the searchLetter ? for eg : suppose: orgText = "abcahaiapaoiuiapplma" searchLetter = "a". I want to hightlight all "a" occurrences in "abcahaiapaoiuiapplma" with red color. Thanks. I wrote this method for my project - SUITextView with highlight : - (NSMutableAttributedString*) setColor:(UIColor*)color word:(NSString*)word inText:(NSMutableAttributedString*)mutableAttributedString { NSUInteger count = 0,

Difference between NSRange and NSMakeRange

▼魔方 西西 提交于 2019-11-28 06:57:05
问题 Is there any difference between: NSRange(location: 0, length: 5) and: NSMakeRange(0, 5) Because Swiftlint throws a warning when I use NSMakeRange , but I don't know why. Thanks for the Help :-) 回答1: The only difference between them is that NSRange(location: 0, length: 5) is an initializer for NSRange while NSMakeRange(0, 5) is a function which creates a new NSRange instance (by using the same initializer inside most likely) and actually is redundant in Swift . Swift has simply inherited it

encodedOffset deprecation

此生再无相见时 提交于 2019-11-28 00:57:27
问题 In my application, I have some code to fetch the range of the host in a URL . It looks like this: private func rangeOfHost(text: String) -> NSRange? { let url = URL(string: text) if let host: String = url?.host { if let range = text.range(of: host) { return NSRange( location: range.lowerBound.encodedOffset, length: range.upperBound.encodedOffset - range.lowerBound.encodedOffset ) } } return nil } Xcode has been warning me that 'encodedOffset' is deprecated: encodedOffset has been deprecated

Find the number of characters that fits in a UITextView with constant width and height with a given string?

北城余情 提交于 2019-11-28 00:15:35
问题 In my application I need to set Read more into the text view if text input is large.so my approach is to find the range of string that would fit in the text view and append See More to it.Is there any way to achieve it in Swift.The requirement is to emulate the read more option to show the complete text in detailed view on tap like facebook. 回答1: The function you are looking for is CTFramesetterSuggestFrameSizeWithConstraints. Essentially, it allows you to find out the number of characters

Looping using NSRange

廉价感情. 提交于 2019-11-27 15:14:11
I'm trying to use NSRange to hold a range of years, such as NSRange years = NSMakeRange(2011, 5); I know NSRange is used mostly for filtering, however I want to loop over the elements in the range. Is that possible without converting the NSRange into a NSArray ? It kind of sounds like you're expecting NSRange to be like a Python range object. It's not; NSRange is simply a struct typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; not an object. Once you've created one, you can use its members in a plain old for loop: NSUInteger year; for(year = years.location; year <