Selecting a word and shows tooltip in iOS - ios

I would like to implement the Medium iOS App like effect for tapping highlight and shows tooltip.
I have been researching on Text Kit and some other stackoverflow questions have some thoughts on it, please also suggest what's the better alternative to this.
Scenario:
Static Text pre-defined
Shows highlights in several words or phrases
Solution thoughts:
Use UITextView for storing text
Use attributed string for text content
Showing background color using NSBackgroundColorAttributedName
Detect the selection by layoutManager.characterIndexForPoint(...)
Shows tooltip next to the selection
Shows tooltip using one of these pods AMPopTip, CMPopTipView, EasyTipView
Right now, I am not able to select the word and shows the tooltip just next to it. Any tier of help is appreciated.

Here is a way to Highlight text.
Create an IBOutlet named myLabel
In ViewDidLoad type
let yourString = "This is how you highlight text"
myLabel.text = yourString
let malleableString = NSMutableAttributedString(string: yourString)
let numberOfCharactersToHighlight = 5
let startingIndex = 1
malleableString.addAttribute(NSBackgroundColorAttributeName, value: .magenta, range: NSRange(location: startingIndex, length: numberOfCharactersToHighlight))
myLabel.attributedText = malleableString

Related

How to implement the large text accessibility to uilabel attributed without losing the font

i have been trying to implement the large text accessibility for attributed text in a label. I have tried a lot of research and found the following way
attributedTextLabel.attributedText = viewModel.attributedString
// accessibility
attributedTextLabel.isAccessibilityElement = true
if let accessibilityLabel = viewModel.accessibilityLabel {
attributedTextLabel.accessibilityLabel = accessibilityLabel
attributedTextLabel.font = UIFont.preferredFont(forTextStyle: .body)
attributedTextLabel.adjustsFontForContentSizeCategory = true
}
But the issue with this approach is that iam loosing the font i have set for the attributed text which consists of multiple fonts in the format of :-
Bold: Simple text
Guys, is there any way to implement large text accessibility for uilabel dynamic text retaining the font of the attributed text. Thanks in advance

Change letter color of tableview if found searchbar text

I have search bar and table view. I can filter table view items based on search bar text. But I have to change found letter's color. For example if I write HE to search bar, I want to show HELLO in table view but the color of HE should different than LLO.
Thanks for ideas.
use NSMutableAttributedString for your concept, Here I tell the logic customize yourself where you need (must call in inside the cellForRow)
lblcheck.text = "hello" // is the original text
lblcheck.textColor = UIColor.red // initally set the whole color for your text
//Create mutable string from original one
let attString = NSMutableAttributedString(string: lblcheck.text!)
//Fing range of the string you want to change colour
//If you need to change colour in more that one place just repeat it
let range: NSRange = (lblcheck.text! as NSString).range(of: "he", options: .caseInsensitive) //he in here use searchBar.text
attString.addAttribute(NSForegroundColorAttributeName, value: UIColor.white, range: range) // here set selection color what ever you typed
//Add it to the label - notice its not text property but it's attributeText
lblcheck.attributedText = attString
Text color change can be done by using NSAttributedString. When you show your search results, in your cellForRow delegate method, get search string and replace the word in your textView with NSAttributedString version. You can change color for the specific string in your NSAttributedString.
This is the general idea without seeing any code.

Multi-line editable piece of text: editable UILabel?

I am trying to create a large plot of editable text but there seems to be 1 option: using a small UITextField.
I know UILabels can be big and wide but I do not know how to make an editable UILabel.
I experimented with UILabel properties and the .layer method but nothing seems to be really working. Anybody have a recommendation as to what I should do?
To summarize, I am looking for a multi-line editable piece of text.
UITextView for the win!!
UITextViews allow for multiple line manipulation of texts and if you use the UITextViewDelegate, it can provide for methods that allow specific things when the textView is clicked on, etc...!
With a UITextView you can provide a specific amount of lines (if you only want 3, you can specify it) and also provide hyperlinks, if need be.
Here is an example I have (changed a little) to show an example for ya...
let textBox:UITextView = UITextView(frame: CGRect(x: firstBox.frame.width*0, y: firstBox.frame.height*0.375, width: firstBox.frame.width*1, height: firstBox.frame.height*0.5))
textBox.backgroundColor = UIColor.clearColor()
let websiteName = "http://stackoverflow.com/posts/38035564"
textBox.text = "SO is an awesome coding site! Please visit\n\(websiteName)"
//No need to set number of lines, it will auto set to as many as needed!
textBox.editable = false
textBox.selectable = true
//Register the hyperlink
textBox.dataDetectorTypes = UIDataDetectorTypes.All
textBox.textColor = UIColor.grayColor()
//Change only the hyperlink part
let textRange = NSMakeRange(textBox.text.characters.count-websiteName.characters.count, websiteName.characters.count)
let style = NSMutableParagraphStyle()
style.alignment = NSTextAlignment.Center
let attributedText = NSMutableAttributedString(string: textBox.text, attributes: [NSFontAttributeName:UIFont(
name: (textBox.font?.fontName)!,
size:13/15*fontSize)!,
NSParagraphStyleAttributeName: style])
attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
textBox.attributedText = attributedText
firstBox.addSubview(textBox)

How to programmatically enter text in UITextView at the current cursor position

I would like to add (some predefined) text at the current cursor position in a UITextView using Swift. So if I have a UITextField named txtField that has some text in it already such as "this is a beautiful valley" and I tap in the area between "a" and "beautiful" so that the cursor is now in the space between "a" and "beautiful" and then click a Button on my user interface a string of text such as "very" will get typed at the cursor position, so that the text in the UITextView will now become "this is a very beautiful valley". At the end of this operation (after button click event) I would the cursor to be just after the word "very". Many thanks for your help. I can see some question on the forum with similar themes, but the answers are in Objective C. I suicidal like help using Swift.
Try this... Inside your button's IBAction use this (please do not use forced optional unwrapping) or else if the textView has no selection or cursor, the app might crash:
let txt = "whatever you want"
if let range = handleToYourTextView.selectedTextRange {
// From your question I assume that you do not want to replace a selection, only insert some text where the cursor is.
if range.start == range.end {
handleToYourTextView.replaceRange(range, withText: txt)
}
}
try this
textView.replaceRange(textView.selectedTextRange!, withText: "your text")
update:
Swift 5.2
let txt = "whatever you want"
if let range = myTextView.selectedTextRange {
if range.start == range.end {
myTextView.replace(range, withText: txt)
}

Adding underline to UILabel attributed string from the storyboard fails

From the storyboard I select the UILabel in question
Then in Attribute Inspector > Label > [I choose] Attributed
Also in Attribute Inspector > Label > Text> [I select the content]
Then I click on the font icon and choose underline
Basically, any change that I select from the Fonts window that pops up does not take effect.
Has anyone ever successfully add underline from the storyboard?
Note: I already know how to do this in code. I want to avoid adding code.
Here is the solution in storyboard:
Open the Attribute Inspector (make sure label is selected), Change the dropdown value from 'Plain' to 'Attributed'. Now a a small text editor will be visible under the font of the label. Select the text of the label, right click and change the font to 'underline'.
I also have attached a screenshot and successfully underlined a text using storyboard in XCode 7.0.1
Underline your text in TextEdit(cmd-U) and copy-paste it in Attribute Inspector > Label > Text > Attributed.
Select label -> Attribute editor -> Title = Attributed
Select the text of the label in the text view in the editor -> Right click -> Font -> check Underline
If the change is not visible -> resize the label
You can make text underline from storyboard like this.
But when you change the text programatically it will override and underline will gone.
So if you have to change the text on runtime. Do this.
For Swift 3
let text = myLabel.text
let textRange = NSMakeRange(0, (text?.characters.count)!)
let attributedText = NSMutableAttributedString(string: text!)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
myLabel.attributedText = attributedText
For Swift 4
let text = myLabel.text
let textRange = NSRange(location: 0, length: (text?.count)!)
let attributedText = NSMutableAttributedString(string: text!)
attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
myLabel.attributedText = attributedText
Leave it set to plain. Make all of your changes. Then change it to attributed.
The Designer Way - Highly Customisable Version
I personally prefer to customise my design(s) as much as possible. Sometimes, you will find that using the built-in underline tool is not easily customisable.
To do something like the following, I have posted an example in How to make an underlined text in UILabel?

Resources