I am using TTTAttributedLabel for URLs and phone number in my app. Every thing is working fine but the problem is that now I need to show icon in the UILabel. Before I am using given below code. But due to use of TTTAttributedLabel now NSTextAttachment is not showing in TTTAttributedLabel. TTTAttributedLabel does not support attachment. So any idea to support URLs, phone numbers and icons in UILabel?
now i decided to show unicode image in TTTAttributedLabel. and it is working fine.
It's better to use UITextView and set the textView's data detector types.
textView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
There is one delegate methods that is called when the link or url in the text view is selected
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
This gives you the URL tapped inside your text view and you can perform your desired action.
Related
I'm having an issue. I am using the Hakawai framework in an app so that I can have mention support (#username).
The issue I've run into is that the textfield I am using is not registering the case where there is no text and a user types an emoji into the textview. As we are using HKWTextView, I believe the textViewShouldChangeTextInRange delegate method is never called, even if implemented. The only replacement I can think to use is :
- (void)textView:(HKWTextView *)textView didChangeAttributedTextTo:(NSAttributedString *)newText
originalText:(NSAttributedString *)originalText
originalRange:(NSRange)originalRange;
in HKWTextView, but that's still not picking up on emojis being typed in when no other text has.
The functionality I would like is:
- Text view is empty
- user types in anything, emoji included
- textview width shortens, "Post" button appears.
Right now, typing emojis into the empty text view will not make the post button appear. However, it's worth mentioning that once the emojis are typed in, if there is more than one, deleting one of them WILL make the post button appear. I'm at a bit of a loss here.
I found the answer to this - It turns out that HKWTextView does some rewiring of the UITextView delegate methods that are fired. Try handling the input in the UITextView delegate method textViewDidChangeSelection. That method will be fired when an emoji is typed.
So I'm trying to create a view where part of the entered text becomes a link to another UIView. I was searching a lot and found the ClickableSpan class that enabled to click on part of the text. It's like in Instagram where when a user types #username or #hashtag it creates a link to another view. I'm just wondering how to give an action to a portion of a text. The text can be in either UILabel or UITextView. Do I use AttributedString, or what?
You can do that for UITextView by using the dataDetectorTypes property.
Objective-C:
self.textView.dataDetectorTypes = UIDataDetectorTypeLink;
Swift
textview.dataDetectorTypes = .Link
I have a UILabel whose text I am getting from a server. Some of the text is to be identified as links, and on touching those links some action should be performed. e.g.
NSString *str = #"My phone number is 645-345-2345 and my address is xyz";
This is the complete text for UILabel. I have only one UILabel for displaying this text (Text is dynamic. I just gave an example.). On clicking these links I need to perform actions like navigating to some different screen or make a call.
I know that I can display such text with help of OHAttributedLabel. And the links can be displayed as follows :
[label1 addCustomLink:[NSURL URLWithString:#"http://www.foodreporter.net"] inRange:[txt rangeOfString:someString]];
But I wonder how can I make these text links perform some action like navigation to different screen or making a call.
Let me know if more explanation is required.
You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you'll intercept later:
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = #"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:#"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:#"action://show-help"] withRange:r];
Then, in your TTTAttributedLabelDelegate:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:#"action"]) {
if ([[url host] hasPrefix:#"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:#"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}
}
TTTAttributedLabel is a fork of OHAttributedLabel.
If you want a more complex approach, have a look to Nimbus Attributed Label. It support custom links out-of-the-box.
You can use UITextView with Phone numbers and links detection YES, scrolling disabled YES user interaction enabled YES, instead of UILabel.
My project has successfully used OHAttributedLabel for this. Check out the
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;
method in OHAttributedLabelDelegate (link). It allows you to decide what happens when a link is clicked. If you look at the source for the example from the OHAttributedLabel project, it's used to display an alert. If you returned NO in this case (to keep the default action from happening too), you could just do whatever you wanted like navigation, etc.
Note however that this requires that you can determine the action correctly just from the text. For our project, we used a slightly fancier solution, where the server sent us text with tags in them and a list of commands to perform for each tag.
There a project called FancyLabel that is about what you need. It might need some customization though.
Also, I think Three20 has this functionality, but it might be an overkill if you don't already use it.
There's also a much simpler solution, if all of your links are phones \ addresses \ urls. You can simply use a UITextView instead of a UILabel. It has auto detection of phones, address, etc. (just check the boxes in IB)
You can also have custom actions in response to click events on those links by overriding openURL, as explained here
Is there a specific reason that you must use a UILabel instead of a UITextView?
Note that a lot of the implementations of attributed labels inherit from UIView or don't implement all of UILabel's functionality.
You can use custom button to give a look like of link ..Also you can add gesture on the custom label if you dont want to use button ..
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];
I'm not a fan of being forced to use UITextView or a third party lib when all I need is a lightweight label that renders links (and that tells me when they're tapped!)
Here's my attempt at a lightweight UILabel subclass able to detect link taps. The approach is different from others I've seen in that it gains access to the UILabel's shared NSLayoutManager via a delegate callback added to NSTextStorage via a category extension. The beauty is that UILabel performs its native layout and drawing - other UILabel replacements often augment or replace the native behavior with an additional NSLayoutManager/NSTextContainer.
Probably App Store safe, but somewhat fragile - use at your own risk!
https://github.com/TomSwift/TSLabel
I have implemented a UITextView where i can underline the text, sometime the text is not underlined at the right place it crosses the words and sentences, is there a default method which underlines the sentences at the right places avoiding the user to maintain the accuracy while touching the screen. help greatly appreciate please share your views.
I would use a UITextView with attributed text and modify the attributes as needed.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:#"All work an no play makes Jack a dull boy."];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:#(NSUnderlineStyleSingle) range:NSMakeRange(15, 4)];
self.textView.attributedText = attributedString;
where self.textView is an IBOutlet to your UITextView and the values are the NSRange are determined by your user interaction.
The problem is that internally a UITextView is a UIWebDocumentView (aka WebKit) and HTML has different rules for underlining words than normal text editors. See my analysis here: http://www.cocoanetics.com/2012/12/uitextview-caught-with-trousers-down/
The approach to set the attributedString the the problem that you would only be able to set the entire text.
Since we do not have direct access at the text container inside UIWebDocumentView and because of the intricacies of HTML you cannot hope to achieve a perfect result.
You can only solve this problem by rendering the text yourself with CoreText. My open source component DTCoreText could help with the display. I am also selling a commercial component for rich text editing that is based on that: http://www.cocoanetics.com/2012/12/dtrichtexteditor-1-1/
PS: he standard method for toggling the currently selected range of text underlined is toggleUnderline: defined as such:
#interface NSObject(UIResponderStandardEditActions) // these methods are not implemented in NSObject
- (void)cut:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)copy:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)paste:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)select:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)selectAll:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)delete:(id)sender NS_AVAILABLE_IOS(3_2);
- (void)makeTextWritingDirectionLeftToRight:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)makeTextWritingDirectionRightToLeft:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)toggleBoldface:(id)sender NS_AVAILABLE_IOS(6_0);
- (void)toggleItalics:(id)sender NS_AVAILABLE_IOS(6_0);
- (void)toggleUnderline:(id)sender NS_AVAILABLE_IOS(6_0);
#end
This is exactly what the context menu adds if you enable attribute editing. If you are not happy with the result here, then my above statements are the way to go.
I have a Tableview and tableview cell is customized to have a UILabel. The text in UILabel is having URLs. Is there a way to detect urls like how UITextView will enable detect URLs so that user interaction should be able to load the urls.
If you just want to identify the URLs, you can use NSDataDetector with the NSTextCheckingTypeLink checking type.
If you want to draw the URLs differently, and you are targeting iOS 6, you can use an NSAttributedString to turn the URLs blue or underline them or whatever. If you're targeting an older version of iOS, you will probably want to look for some free code on the Internet to draw styled text, like OHAttributedLabel.
If you want to actually make the URLs touch-sensitive, you can add a tap gesture recognizer to the label and try to figure out which part of the string was tapped (somewhat complicated), or look for some free code on the Internet that already does it for you (like OHAttributedLabel), or just put a UITextView in the table view cell instead of a label.
as Rob point out how can we achieved the same is awesome.
But, we can use a tact so can save with issue of ios version (possibly), just by using a UILabel and UIButton.
What we need to do is that, either from IB or story-board,just place a UILabel with string as URL(use this as title), say;
"www.myURL.com"
Now, just above it, place a UIButton(button with custom type), and just use "______"(underline) and set this button as overlap your UILabel and also the 'underline' must be beneath your label.
Now, just do in action of button whatever you required as you need on the click of URL and here also you can change the textColor, etc, properties; also load URL and navigate to UIWebView.