I add URL to attributed string in OHAttributedLabel ("forgot password" link to www.xxx.com ") . Everything looks perfect except that when I clicked on that string , nothing happen.
It should open that URL in safari. And then when I tried to add delegate method.
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo
This delegate method is not called at all. The delegate is set properly because I tried another delegate method and it is working
-(UIColor*)attributedLabel:(OHAttributedLabel*)attributedLabel colorForLink:(NSTextCheckingResult*)linkInfo underlineStyle:(int32_t*)underlineStyle;
I setup UILabel via Interface builder and change its class to OHAtrributedLabel. And here is my code
//forgotLabel.text is "Forgot Password"
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:forgotLabel.text];
[attr setLink:[NSURL URLWithString:#"http://www.yahoo.com"] range:NSMakeRange(0, attr.length)];
//forgotLabel is OHAttributedLabel
forgotLabel.attributedText = attr
forgotLabel.delegate = self;
Can somebody help ? I don't want to use TTTAttributedLabel because in my project I used OHAttributedLabel in tablecell and it work perfectly. So I don't want to add another 3rd part compnonent that does almost exactly the same thing .
Thanks in advance.
I solved this myself by adding
forgotLabel.catchTouchesOnLinksOnTouchBegan = YES;
Related
I have a tableView with tableViewCells cell that show a textView.
textView uses an attributedString with custom URL link information, set up in tableView:cellForRowAtIndexPath: as shown in this tutorial:
NSMutableAttributedString *attributedDisplayString = [[NSMutableAttributedString alloc] initWithString:displayString];
[attributedDisplayString addAttribute:NSLinkAttributeName
value:[NSString stringWithFormat:#"username://%#", userName]
range:NSMakeRange(0, userName.length)];
cell.textView.attributedText = attributedDisplayString;
When I tap the link, textView:shouldInteractWithURL:inRange: is called in the delegate, thus the custom URL link has been detected and responds.
However, the supplied URL is nil.
What am I missing?
Sorry for asking too fast. I found the problem, but maybe this helps others:
My variable userName simply contained a space, and could thus not be converted to a URL.
After removing the space, it works.
To make a string that can be used for a URL, one can use in iOS8 and earlier
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
and in iOS9
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]
I'm trying to get the title of a String from the content of a UITextField, so you can always edit it and save it.
I've already linked the String and the UITextField, but everything works just if you edit the content of the UITextField in the storyboard, and it doesn't work in the application (Simulator and Devices).
This is what I'd like to do:
const NSString *nameOfTheString = #"the content of the text field, that you can change when you want from the application"
Here there is the code that I have:
nameOfTheString = nameOfTheTextField.text;
I Hope You'll Help Me.
I'm using Xcode 6 (Objective-C)
You can't change nameOfTheString if it is a const (constant). If you want to change nameOfTheString, simply use NSString *nameOfTheString; to declare it.
Remove the const from the declaration (const means it can't be changed):
NSString *nameOfTheString;
Then use a delegate method of UITextField to update the value of the string whenever it is changed:
- (void)textFieldDidEndEditing:(UITextField *)textField {
nameOfTheString = textField.text;
}
UITextField delegate ref:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html
I'm developing application similar to Vine Application, in that comment cell is giving navigation (#User is there or #tag is there) that is exactly working if user or tag is there otherwise not.
If anyone know, how to do that please give me reference or suggestion (Please don't give any suggestion like if word found with # or # then give string attribute that I already done that but I want exactly vice comment cell )
You can give some idea or suggestion also.
Thanks is advance.
You can use TTTAttributedLabel. It will help you to provide link action.
Implement it as follows
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:frame];
[label setAttributedText:#"yourText"];
label.delegate = self;
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
[label addLinkToURL:url withRange:range];
[yourCell.contentView addSubview:label];
and you can capture the link action in:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
which is the TTTAttributedLabelDelegate method. Don't forget to add TTTAttributedLabelDelegate in the interface file.
I want to change Place holder color from user attributes (interface builder) because i dont want to subclass my UITextfield etc... i searched on internet and found this "_placeholderLabel.textColor" Yes this works well Programmatically [self.tfEmail setValue:[UIColor whiteColor] forKeyPath:#"_placeholderLabel.textColor"]; but i want to use it in user attributes in interface builder.
Try removing '_',
Works for me.
Without USING USER ATTRIBUTES
self.txtField.attributedPlaceholder = PlaceHolderAttributedString(#"ABCDEF");
where PlaceHolderAttributedString is a macro defined as
#define PlaceHolderAttributedString(placeHolderText) [[NSAttributedString alloc] initWithString:placeHolderText attributes:#{NSForegroundColorAttributeName:ColorTextFieldPlaceHolder}]
USING USER ATTRIBUTES
placeholderLabel.textColor
in swift:
one_textfield.setValue(UIColor.redColor(), forKeyPath: "_placeholderLabel.textColor")
I am using the storyboard and have a view where I have subclassed some UITextViews.
My problem is that I am using ibtool --generate-strings-file to extract strings from the storyboard for localization and afterwards use ibtool -write on another storyboard file to apply the translated strings.
When I use ibtool any UITextViews that have attributed text is ignored by the ibtool --generate-strings-file command and omitted from the resulting strings file.
Is it possible to extract attributed text from a storyboard for localization?
on Xcode 6.1, the best way is to copy the attributed text of a text view into a “BASE” RTF text ( using TextEdit for example or directly from XCode > New File > ressources > RTF ).
Going through the TextEdit way, you need to import your text into your project. Obviously, if you have done it through Xcode, nothing to import.
then just you use the utilies panel to find the “localize..." button which will do it's deed for you.
to import the correct version just do ( in viewWillAppear for ex. ),
NSURL *url = [[NSBundle mainBundle] URLForResource:[fileName stringByDeletingPathExtension] withExtension:[fileName pathExtension]];
NSError *error;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithFileURL:url
options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
documentAttributes:nil
error:&error];
[_originalMessage setAttributedText:attributedString];
Update for Swift 4:
var attrString: NSAttributedString?
let fileUrl: URL = Bundle.main.url(forResource: "mytextfile", withExtension: ".rtf")!
do {
attrString = try NSAttributedString(url: fileUrl, options: [.documentType:NSAttributedString.DocumentType.rtf], documentAttributes: nil)
} catch {
// Somebody do something!!
}
I ended up concluding that it couldn't be done using ibtool in the present version.
Instead I let the Textview be a plain text view and, using my subclass, parsed its text property so I could make a NSMutableAttributedString and set the needed properties.
That enabled me to use ibtool to extract the strings and still have an attributed textview.
It wouldn't make sense to localize attributed text from the storyboard as you would need to know where to apply the attributes after the translation. The word order might have changed and where you would have specified some blue bold text for instance might no longer make sense.
You can of course still do it via code where you can split up your string in multiple ones and localize them individually. Then you can specify setAttributes:range: so that you know that the right attributes will always be applied on the right range of the string.
Here's a workaround which I find quite useful: just create a ViewController with the translated TextView. In details (here I just translate the attributed text into english):
1) Crete a new Controller with "New File" -> "UIViewController with Xib". Name it "AttributedTranslated" and fill it with just a TextView with the attributed text being translated, using Interface Builder.
2) In your main controller .m file, write down the following method:
- (BOOL)isEng {
return [[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:#"en"];
}
3) Define an "AttributedTranslated" object and a View in your .h file
IBOutlet UIView *attrView;
AttributedTranslated *attr;
4) On the xib file (or storyboard) of your main controller, create a View containing just the attributed textView (in the original language) and link it to "attrView".
5) On you viewDidLoad do something like the following:
if ([self isEng]) {
desc = [[Description alloc] init];
[attrView addSubview:attr.view];
}
Maybe it's not the best way to do it, but it does allow one to translate the attributed text in Interface Builder, easily!