I am using default UItableViewCell to display text. Now lable of that cell is set to multiline as
defaultCell.textLabel?.numberOfLines = 0
And I have multiple cells containting multiline text. And I want to add links in that text something like below
This is multiLine Text which will lead you to the StackOverFlow. And you will find something there.
I searched and found solutions like using UITextView instead if UILable etc.
But I want to do it with a UILable. Thanks for your help.
You should use UITextView and set Detection for Links:
textView.dataDetectorTypes = UIDataDetectorTypes.Link
Or if you need labels, then you can use some 3rd party solution like this: https://github.com/TTTAttributedLabel/TTTAttributedLabel
https://github.com/null09264/FRHyperLabel
I think that what you really want in terms of UX is a link being represented by the entire cell. You should simply implement tableView(_:didSelectRowAt:) and send the user to the destination there.
Related
I have multiple custom UITableViewCells, each containing a UITextField. On value change of any of those text fields, I want to update a UILabel elsewhere in the view. How do I do that?
The label displays an aggregate of all the text fields' values. The number of cells is dynamic.
(I guess that's very easy, but I'm new to Swift development. My first attempt was to add an #IBAction to the cell class definition, but I got stuck on trying to get a reference to the label.)
Edit:
The accepted answer is absolutely fine and works. I also recommend checking the solution recommended by DonMag in the comments section as it may be better in the long run, although it seems more difficult to implement for a beginner.
This is more of a question of how to go about achieving it rather than a specific solution. I would like to have a label which allowed buttons and textfields to be inline and wrap to the next line. An example is the iOS shortcuts app where you can type in the same block as the text, and the textfield wraps along the same line. It is a textfield as there is a caret.
At a highlevel it might look something like this:
where each view follows the line and wraps to the next when needed.
I originally thought about using an NSAttributedString with links that were styled and the link could either act as a button, or open a textfield for input. I tried this and got something which worked but it did not resemble the iOS shortcuts app where the textfield is within the text. I have also thought about using textkit, but I have not gotten that far with this as I am not sure it is the correct approach.
Thank you for any ideas or solutions.
EDIT
I have thought about another way of achieving this but I don't know whether it would work either. The idea would be to use a collectionView of textfields. Some textfields would share the same LayoutManager so that the text is shared across the textfields. I would have to calculate how many textfields to create so that they flowed down the collectionView
In the image, Label 1 is made up of 1 textfield which has editing disabled. TextField 1 is made up of 3 textfields which have editing enabled and the three textfields would share the same LayoutManager. Label 2 is made up of 2 textfields with a shared layout manager, but editing disabled.
Using this approach would mean calculating how many textfields to create for each "Block" (Label or TextField) and updating this each time content changes. With this approach, I am only thinking of labels and textfields but the button can be added at another time.
I just started on this, but realised that sharing layout managers disables editing so I don't know whether this would be possible anymore.
Thanks
This is non trivial task for sure, and I don't think there's a ready-for-use solution.
Using NSLayoutManager you can calculate frames of each line/character of your text, and then forbid touches depending on these frames, and add background under editable text.
You have to use UITextView, because you gonna need to forbid user to select part of the text, and you can do it using something like willChangeSelectionFromCharacterRange:toCharacterRange: delegate method, and ofc you need shouldChangeTextIn: to forbid removing non editable text too.
I need to create a dialog, similar to the one that is used to write functions in Numbers from APple, for example.
User can select range in the table, which is then represented as any kind of UIView inside text input line. When long-tapped, the content can be edited like normal text, but what is most important is that whole subView, containing text with range description is treated as one character.
Does anybody know or have idea how to achieve this?
I would appreciate any hints...
I had to deal with something similar, to have custom UIView(s) in a search field.
I'm not aware of a "simple" way (ie, API) to achieve your goal. You need to create a custom component that looks like a classic field, but isn't one.
To be a little more specific:
Subclass an UIView (or directly an UIScrollView)
Have inside a TextField (or UITextView depending if it needs more than one line) to handle the "keyboard".
Put some custom UIView/UILabel before the textField/textView and adjust the textField/textView origin depending these views/labels
If you want to have a label/view IN the text, then it's a little more complex and you'll need something even more custom:
Handling the keyboard events without a textField/textView (there is a way but I forgot how exactly)
Drawing the text and adding views/labels in specific locations.
Of course all depends on the component you want to create ^^ there is no one simple answer.
I want a text field similar to the stock messaging app in the iPhone. Text field should have the same look and should be expandable to allow multi-line editing. Could you please suggest a method to get this in my app?
Thanks.
Use a UITextField.
To get the height of the text field use -sizeWithFont:constrainedToSize:lineBreakMode:.
To have the look of the Apple's text field put an image behind the text field that looks like Apple's text field. See -resizableImageWithCapInsets: to get the image to stretch properly.
Have a look at this :
UIBubbleTableView
EDIT :
The previous link was for the "bubble styled" tableview. If you want the view where you actually type the message, this is the one :
HPGrowingTextView
For other people who are searching for libraries but the ones mentioned here are with many bugs
Here is a library:
SlackTextViewController
A drop-in UIViewController subclass with a growing text input view and other useful messaging features. Meant to be a replacement for UITableViewController & UICollectionViewController.
I have added UITextView to an app and the list is kind of long. What is the easy way of searching a word in this list. I can add a button and textfield to enter and search for a word. I was was wondering maybe there is already built feature for UITextView.
Thanks very much
[TextView.text rangeOfString:]; or something like this.
It sounds like you're asking if there's a search UI built-in for UITextView (like the way the system libraries manage some aspects of the searchDisplayController property on UIViewController). There is not a built-in feature for this.
One convenient option you might consider is creating a 44 pixel tall view with your search text field and button(s) and then set this view as the "inputAccessoryView" on the UITextView object. The system will then manage animating its display above the keyboard, and scrolling the UITextView so the user is not typing underneath this view.