How to identify programmatically generate TextViews in swift? - swift

I need a way to identify programmatically generated text views. The problem is that I am creating a list of textviews, and I want to control them with code. How can I set and ID or something to know which one is which?

use tag
let textview = UITextView()
textview.tag = your value in int

Related

How do i randomize between labels when button is pressed?

I'm pretty new to swift. The purpose of my app is to show different text on my labels when a button is pressed. I will have hundreds of labels so should i use some kind of a database. If so how can I randomize between these labels. Would be great if any of you could write that piece of code. I also need my app to remember the previous label, so that a user can go back when another button is pressed.
Here is how to generate a random number in Swift:
let rand = Int(arc4random_uniform(x))
This generates a random number between 0 and x-1.
Unfortunately, without a more detailed description and without your code, I can't answer anything more than that.
Create an array of all the possible labels, i.e.
let labels = ["Label1", "Label2", "Label3", "Label4"]
//add data to the labels
Use randomElement() on the labels array to get a randomLabel, i.e.
let randomLabel = labels.randomElement()
You can do create an Array with labels. For example:
let labels = ["Some Text","Some Text","Some Text"]
Then use randomElement() from labels array:
randomTextLabel.text = labels.randomElement()
You can also use json to store data. I dont know how, but you can always google!

Number of characters that fit in NSTextView

Note: This question is for a MacOS app and not for iOS
I have a business requirement to convert this amazing iOS custom text view(ReadMoreTextView) to macOS(AppKit). However I am finding it difficult to convert it. I was hoping it would be easy since both UITextView and NSTextView use NSTextContainer, NSLayoutManager and NSTextStorage. But it seems like they behave differently on both platforms.
I need to calculate the number of characters or character range that is visible on the NSTextView. I am trying to use the following method but it is always returning the complete character range instead of visible character range(actual text length instead of visible text length). I couldn't find any other method in layout manager which can help on this. Please let me know if you have any pointers on this.
Here is the code:
extension NSLayoutManager {
func characterRangeThatFits(textContainer container: NSTextContainer) -> NSRange {
//this is the current maximum number of lines
//container.maximumNumberOfLines = 3
var rangeThatFits = self.glyphRange(for: container)
rangeThatFits = self.characterRange(forGlyphRange: rangeThatFits, actualGlyphRange: nil)
return rangeThatFits
}
}
Answers in Swift and Objective C are welcome.
Update:
Please ignore any scrolling on the textview since I am disabling all types of scrolling. My intention is to have a view with "... More" button at the end of it then on tap of it the view gets expanded. For this reason NSTextField based solution is also welcome if we can find visible characters in that.
You can get the visible portion of the NSTextView using its visibleRect method. From there, you just need to get the text range that falls within that rect. So, if your view supports vertical scrolling only, something like this should work:
let visRect = textView.visibleRect
let layoutManager = textView.layoutManager!
let container = textView.textContainer!
let glyphRange = layoutManager.glyphRange(forBoundingRect: visRect, in: container)
let charRange = layoutManager.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
Be aware that if your NSTextView supports horizontal scrolling rather than just vertical, you'll have to do a little more work than this, since you'll possibly be looking at a non-contiguous block of text. In that case, you'll have to get a bunch of smaller ranges for each visible line fragment instead of just grabbing the whole range like this.

Get the values in picker from server

I want to get some values to the picker dynamically. Statically it is performing well. But when I want to add picker row dynamically from calling web service, there is an occurrence of NSRangeException. Whether the array having data and I am able to alert that data. I am using Titanium SDK for this iphone application.
if(gameTypeName.length>0){
alert(gameType.length);
picker.add(gameTypeName);
}
The array is creating as:
var typeName = college[j].GameTypeName;
gameTypeName.push(Titanium.UI.createPickerRow({title:typeName}));
Whether it is working fine with static data as:
var picker_data = [
Titanium.UI.createPickerRow({title:'Title 1',value:'1'}),
Titanium.UI.createPickerRow({title:'Title 2',value:'2'})
];
picker.add(picker_data);
Believe it or not, the Picker doesn't have a "Value" property.
You can use pickerView. But, easy to use Table View. you set array data in you TableView (tableView.data =;).
and for get value
tableView.addEventListener('click',function(e){
// own your requirement
Ti.API.log(e);
});
I think this is easy to use and good looking.
If, you want to show and hide. then you can use animation or window.modal property.
var win = Ti.UI.createWindow({});
win.add(tableview);
win.open({modal:true});

UITextView or UIWebView: How to display pages of a simple editable TXT file

All I want to have is a full-screen simple text editor. However, I don't want the scrolling component, but rather let the user flick through the pages (instead of scrolling). So I need to import or open a TXT and then format it by braking it down (e.g. by dividing its contents to 10 lines per screen/page).
My question is how I will display the txt? UITextView is scrollable (even though I can disable this in IB)... I did not find any method for UIWebView to let it format my contents on different 'pages' or screens.
Where will I need to start? Ideally I'd need some sample code. All the samples for UIWebView do not tell me anything about how to format editable text on several pages.
So all I really want is an UITextView which is not scrollable and opens up a new page/screen if I run out of space on the first page/screen.
Thanks for any help to get me started.
first thing first ...... there is no particular method to achieve this
1.You need to break your single string into multiple strings, to do that you can use
int pageNumber; // suppose this keep track of on what page you are
int count; //suppose this keep track of how long string your one screen support
NSString* completeString; //suppose this is your string
NSRange range = NSMakeRange(pageNumber * count, count);
NSString* temp = [completeString substringWithRange:range];
2.Now instead of using UITextView (if you don't want user interaction ) you should use UILable
just change the property of UILabel (this one is of your interest)
UILabel* myLabel; //suppose this is that label
myLabel.numberOfLines = 0; //this will chage your label to go multyline.
You should be able to achieve this by putting your UITextView into a UIScrollView and setting pagingEnabled on the UIScrollView to YES.

Custom iPad 10-key popover possible

and thanks for your responses in advance.
I have been looking around for the possibility of having a 10-key, numeric only, input available when a user clicks on certain fields that do not require the use of the full size keyboard.
I know that popovers can properly display custom types of input, but does anyone know if there is a way for a standard 10-keypad to display in a popover? And if so, can you point me in the right direction.
Again, thanks in advance.
-Rick
You don't need to use popover if you try to enter numeric value in a UITextField/UITextView.
You can replace the keyboard by your own view.
You just do :
[yourTextField setInputView:myInputView];
You can have a look at KeyboardAccessory sample from apple
http://developer.apple.com/iphone/library/samplecode/KeyboardAccessory/Introduction/Intro.html
In this example, they provide an accessory view to the input view but if you modify the code and set inputView instead of inputViewAccessory, you should be able to do what you need.
If you use a UITextView, you can append text where the carret is with the following code:
NSMutableString *text = [textView.text mutableCopy];
NSRange selectedRange = textView.selectedRange;
[text replaceCharactersInRange:selectedRange withString:#"\n"];
textView.text = text;
If you use a UITextField, it seems you can only append text at the end of the string (no mean to retrieve carret position).