Format UILabel with image and links - ios

How can I add a string with link and image in a UIlabel using swift code.
Below is a sample what I needed:
Visit to the following link Terms and conditions 💼 for specific
instructions.
I need the Terms and conditions and the bag in red collar and remaining text in grey color. Tapping on red coloured text opens a link.
Anyone please help.

The short answer is, don't do that. A UILabel displays a string. That's what it's made to do. Adding images and links to a label is stretching it well beyond its intended purpose.
You can take a generic UIView and add subviews for your text and your image(s). Build your desired contents out of the elements you need.
I would suggest using a UITextView since that supports clickable links. Set it's editable flag to false and turn on link detection.

As per your problem described here. I have found a perfect solution, as per your requirement.
Here is the link to your solution : Link

You can try this and bridge it as it's written in Objective-C
KILabel

I solved it by adding an attributed text and user interaction to handle link tap. To add image NStextAttachment can be used. Write method to handle tap gesture for label using range.

Related

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes"
It causing this underline in application (first picture with enabled setting, second without)
Any idea how to programatically or in storyboard disable it?
I tried attributed text but I get same result :(
I'm newbie in Swift.
Thanks for help!
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
I found this answer works with SWFrameButton
And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...
I totally agree with #maddy's comment:
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
But I did stumble on a way to accomplish the task at hand...
In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.
Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.
see the Attributes inspector for UIButton here.
Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.
You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.
Are you sure you want to do that?
Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.
I found the solution. All you have to do is set a picture as the background of the button. just pick a picture with the same color as the button you created.

how to add button in label text

I don't know if this is applicable in iphone. I'm trying to add button inside label text. See image below.
If you see there is href or anchor(web name) inside the square that I'm pointing. Can I put button inside the text? These data came from rest api. I need an advice if this applicable or possible to do that. If ever this is applicable please give me idea on how to do that.
Thanks in advance
I think what you want is actually add user interaction to certain range of the string, right?
This can be easily achieved by this library TTTAttributedLabel
Please take a look at it and then you can dynamically set what you want to link.

iOS Add Image to TextField

I am experimenting a little bit witch apps programming.
How I can add a UIImage to a UITextField"?
Like Whats App or the most other Chat Apps.
I have search some time but doesn't find a solution, also i have tried to search in side of XCode with the auto complete but I have not found a function. So I hope you can help me.
The easiest way to add an image to a text field is to have a UIView that contains both UITextField and a UIImageView as subviews.
Next up, you can have a UIButton with both an image and text, and just set userInteractionEnabled to "NO" and it'll behave like a text view with an image next to it.
Now to get more complicated, if you want a chat-like text field that allows text (that you can type into) and images next to each other, you need to start thinking about custom subclasses. Other people have asked and have gotten answers for this same approach.

Is it possible to 'hyperlink' text within a UILabel/TextView but activate a segue on tap of this 'hyperlink'?

I say 'hyperlink' because I don't know what else to call it, and that is how i'd like it to appear.
Obviously this is possible using a combination of labels and buttons, but my labels and buttons are programmatically generated and I imagine i'd have to also programmatically arrange them, which would likely be tedious and inflexible in terms of changing font sizes etc.
Any ideas/approaches would be much appreciated!
As an example, look at Instagram's following and news feed:
You should set userInteractionEnabled and then add a UITapGestureRecognizer to the label.
Have a look at Nimbus Attributed Label it can provide the functionality you are looking for.

How to detect and make hyperlinks/mentions/hashtags clickable in UILabel?

How to detect and make link/mention/hashtag clickable in UILabel. Alternatively, is there any open source library that I can utilize (I already looked at Fancy UILabel which doesn't handle multiline tex, TTAttributedLabel which doesn't handle mention/hashtag)?
There is no way to do so with UILabel in the current iOS...
TTTAttributedLabel will let you style up your label, however for clickable (or rather - tappable) links you should rather either use a UIWebView and style it in such a was as to disguise it as a Label, or, you could get geeky and split your labels up and use a UIButton in the mix, but that's very messy - like a puzzle, only... they don't fit together.
Last option you might have is to overlay a UIButton over a link, but this requires that you know where the link is and since the question was about detecting links etc...
You should really look into UIWebView.
You can achieve this using following library:
https://github.com/SebastienThiebaud/STTweetLabel

Resources