How to Create Dynamic String Object - ios

In my Application, I need to save a UITextView's text in NSAttributedString.
When a user swipes on the screen then whole text in UITextView will copy in NSAttributedString and UITextView will become NIL and then after some writing in UITextView if User will do again Swipe then that Text will also store in another NSAttributedString.
My problem is, I don't know how many times User will do swipe.
So, i have to take as many string as many times User will do swipe on screen.
I need to take something Like this
For Example...
Int var = 5;
NSAttributedString *strObject(var);
So, actually NSAttributedString object is strObject5
"OR" May be Like This...
for(Int i=0;i<=var;i++)
{
NSAttributedString *strObject(var);
}
I knew that, this all things are wrong but i need to do something like this.
Any idea, link, code, tutorial will be great Help.....

Related

Is scrollRangeToVisible(_:) considered slow if a large attributed text is set in UITextView?

I have a question regarding performance and good practice considering scrolling to bottom in UITextView.
For example, I have a UITextView that displays attributed text, like log messages, with a specific font and color. Note that the log text can include up to 50.000 logs. Every time I append a new log (new attributed string) to existing UITextView attributedText property, I need to perform a scroll to bottom.
The function below works very well:
private func scrollLogTextViewToBottom() {
let bottomRange = NSRange(location: textView.text.lengthOfBytes(using: .utf8), length: 0)
textView.scrollRangeToVisible(bottomRange)
}
I have concerns regarding performance and potential memory (or other resource issues) regarding calling this function to scroll to bottom when UITextView have large quantities of logs, lets say 50.000? Is there a more faster/performance friendly solution to scroll to bottom for each added log, that to always call to get range of the entire UITextView?
So, every time you append a new log (a text), it will be appended in the textview's text as a whole text (without any separation)? I guess this is a good solution.

Accessibility (VoiceOver) on NSAttributedString

I have a block of text in a UITextView built using NSAttributedString. I require VoiceOver to say something extra when certain portions of the text is touched.
Is it possible to add accessibility attributes to NSAttributedString?
I think one solution to this would be a custom subclass of UITextView. Then you can return an array of UIAccessibilityCustomAction in an override of accessibilityCustomActions. These objects not only describe the actions taken by the text view, but they also allow VoiceOver to execute them.

How do I bind an NSString to a UITextView in Reactive Cocoa?

I'm building an iOS social client, and in the "compose" view, I've got a UITextView where the user enters text. I'd like to use ReactiveCocoa to bind the text of the UITextView to the NSString of the data model, to follow MVVM.
However, I'm running into several issues, all related to a single thing: the RACObserve block doesn't get called when the UITextView's text is changed programmatically.
(An example: I change the text into an attributed string to highlight #hashtags, #usernames, etc, but this attributed string doesn't get created when the view is programmatically changed.)
In my previous question on this topic, I got some helpful advice that I should bind the textview to the model - and vice versa - but it's not clear to me how I should do it with the current version of Reactive Cocoa. The sample code that I've managed to find calls APIs that are now deprecated.
What's the appropriate way to bind the rac_textSignal of a UITextView to an NSString (and vice versa) so that I can reliably call a block of code when the contents of the UITextView are changed (whether programmatically or by the user)?
The answer depends on whether the binding between the view model's text and the UITextViews text needs to be bidirectional. Generally we try to stay away from bidirectional bindings because they become harder to reason about. Ideally only one direction is driving the data.
So in that case, you'd write something like:
RAC(self.viewModel, text) = [RACSignal merge:#[
[self.textView rac_textSignal],
RACObserve(self.textView, text),
]];
That way you're picking up on changes to both the UITextViews text property directly, and text changes that come from the user typing.

iOS Instagram Tags Text Layout? How Is It Done?

I've always been curious how instagram gets it's tags to layout fluidly and wanted to know what goes behind this functionality? To me, it seems like they're using UIButton's and just calculating placement of the buttons line by line? Is that right? Or is there a simpler method of doing it without having to manual calculate the placement of each tag line by line / or side by side given that multiple tag's width's fit together?
Or, are they using NSAttributedStrings?
Any ideas?
Here's an example of the layout I'm referring to.
Well, I'm currently working on NSAttributedString and UITextView to get all this working, and my current code can do it. So I'm going to explain a possible version of doing this.
Here are the tips and big steps, with iOS7:
NSAttributedString, in iOS7 has a new attribute : NSLinkAttributeName. You can add it to your NSAttributedString, and with a detection method (combining: how to make #key and #key clickable in IOS7 and Get word from tap in UITextView) you can manage it. That's for the general way of doing it. For previous version, you'll have to do a little more of work, but keeping this general idea.
But since, it only for hashtags, since you can detect the world behind the tap, you can do this: Detect what word has been tapped (see previous links), detect if this word has an hashtag, and if yes, you can do whatever action you want. You can play with a NSRegularExpression to know where are the hashtags, and know when to "color" them.
Here is a simplified way to do it using UITextView and NSAttributedString:
Use regular expressions to get character ranges for links.
Add link styling to the matched ranges.
Add custom attribute to range that references tap action to execute.
On touch, if index of character is inside a matched range, change the link formatting to highlight it and run the associated tap action.
How to get index of character at location in iOS7:
- (NSUInteger) getCharIndexAt: (CGPoint) location
{
NSLayoutManager *layoutManager = self.layoutManager;
NSUInteger characterIndex = NSNotFound;
characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:self.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];
return characterIndex;
}
Also check out WWDC 2013 intro to TextKit Demo
ios7 text utilities

Can I place noneditable text in UITextField

I'd like to place some fixed text inside an UITextField, but before the insert point, sort of like this:
| He went (towaards)
...where "towaards" is the editable part.
The point is to show the editable text in context.
Is that possible, and/or are there better alternatives?
I guess you can do that by implementing UITextFieldDelegate. There is one method – textField:shouldChangeCharactersInRange:replacementString:.
In this method you can check each and every character which is entering by user (even backspace). So first you implement change in another NSString object then validate with your condition.
Check whether new string passes through your validation then replace the old string with new string, otherwise leave it as it is.
For example, check new string starts with your string or not. You can use this:
[myString hasPrefix:#"He went"];
Here in the same method you can also do one more thing, when user taps on UITextField check if #"(towaards)" string is there in textField then remove that text by code.
Hope this is what you want.
Just set the text property of the label to #"He went (towaards) and when editing is started change it to #"He went "
I don't think you can put noneditable and editable text in the same UITextField.
What about putting a UILabel with the noneditable text right beside the UITextField ?

Resources