how to make a word as hyperlink in iphone? - iphone

In my app, I have 4 values 0, 10, 20 and 30. each value contains diff url's.
I used UITextView to trigger the url when user taps it, but for value 20 I need a word should be present there instead of direct url.
Now the user will tap on that word it should connect to that url for value 20. In-short I want a word to be tapped that will connect to some url.
Any ideas???
Here is my code:
NSString *dot=[NSString stringWithFormat:#"%#",addressInfo.category];
NSString *dotstr=#"20";
if ([dot isEqualToString:dotstr] ) {
urlblue.text = #"eBrochure";
self.urlblue.dataDetectorTypes = UIDataDetectorTypeLink;
}
whenever user taps "eBrochure", my app should opens the associated url.....?

Yes,U can use UIWebView to do that.But if u just want to use Textfield,I think the link can help u.
Tappable URLs in Core Text
^-^

You can draw it using this code:
CGRect underlineFrame = CGRectMake(textLabel.frame.origin.x, textLabel.frame.origin.y+textLabel.frame.size.height, textLabel.frame.size.width, 1);
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=underlineFrame;
viewUnderline.backgroundColor=[UIColor blackColor];
[self addSubview:viewUnderline];

Related

How to make UITextView text all selectable and show copy option to the user iPhone app?

Am working in iPhone application like iMessage native app. I have designed a page look like iMessage with added a bubbles also. How i done is, just added a UITextView in UITableView Cell and also i have added UIImageView on UITextView with bubble images.
Load the text to UITextView from NSMutableArray. It is working fine. Now my doubt is:
In iMessage app they set that when the user hold the bubble they making the text as selectable and showing Copy option. In my app when the user hold a bubble the Copy option showing but some particular text only copying. How can i select all text and show copy option the user?
In iMessage app the selecting region (two lines in beginning of selection and end of selection) not showing. But in my app the selection regions are showing how can i manage that?
Could you please help me to solve this issues? Thanks in advance.
Finally i have solved my problem used below code. Now am able to select all content from UITextView and showing Copy option to the user to copy the message. Please find the code for your reference.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
[self selectAll:self];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
- (void)copy:(id)sender
{
UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
[pastBoard setString:self.text];
self.selectedTextRange = nil;
}
Happy Coding.
Here, How you can select text in UITextView
[textView setSelectedRange:NSMakeRange(row, length)];
where, row indicates from which row you want to start your selection. length is total length of text.
e.g. [textView setSelectedRange:NSMakeRange(1, 45)]; // where 1 is first row, and 45 is length of text of selection.

How to set uitextfield to italics Xcode 4.5

I have an app where the users enters information in a few textfields which are then added to a uitextview by a nsstring does anybody know how i can set one of the textfields to italics and keep the others normal
regards
Edit:
Heres my code i want to only change once textfield (e.g textfbookpublisher):
NSString* combinedString = [NSString stringWithFormat:
#"%#,%#.'%#.'%#.%#,%#.",
textfbookpublisher.text,
textfbookauthor.text,
textfbooktitle.text,
textfbookplace.text,
nameofuni.text,
textfbookyear.text];
DissertationGen*bookg = [[DissertationGen alloc] init];
bookg.message = combinedString;
bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:bookg animated:YES]
A quick search would have given you an easy answer. For example, this post.
Simply set the font of the UITextfield you want to a [UIFont italicSystemFontOfSize:].
try this link see accepted answer and also check TTTAttributedLabel library
Bold one Word
now you want only textfbookpublisher in italic then pass it individually to next DissertationGen view
in DissertationGen controller's viewdidload method use following code
UILabel *lblpubl = [[UILabel alloc] init];
lblpubl.font = [UIFont italicSystemFontOfSize:18.0];
lblpubl.text = your variable which contains textfbookpublisher's text;
now use lblpubl as you wish

Calculating UILabel and UIButtons sizes to make a sentence?

I'm trying to show in my app something like this:
Liked by #user1, #user2 and 3 more.
Many apps have this format either for comments like who posted it and their comment,
example: #me yeah apples are good
When you tap their name, it takes you somewhere in the app.
I would like #user1 #user2 and 3 more to be clickable and perform a selector.
I would also like them to be bold and a certain color.
UIWebView can stylize text but can I perform selectors by touching part of an attributed string?
I have been trying to find the best way to do this without making labels and buttons and calculating them dynamically by the length of each username like this:
Label = "Liked by "
Button = "#user1"
Label ", "
Button = "#user2"
Label "and "
Button "3 more"
Label "."
There must be a better way!
Thanks in advance.
You'd be better with an inline UIWebView -- provided you don't need to add this to a scroll view. You can detect actions/link clicks inside a webview by registering some delegate for them and then giving fake protocols/URLs as the link URL. Something like this:
UIWebView *wv = [[UIWebView alloc] initWithFrame:aFrame];
wv.delegate = self;
[wv loadHTMLString:#"<a href='userlike:user1'>#user1</a> hates this." baseURL:nil ]; // further parts of this method name omitted
// because it's long, look it up in UIWebView class reference.
[self.view addSubview:vw];
[wv release];
And the delegate method:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq navigationType:(UIWebViewNavigationType)type
{
if ([[[rq URL] protocol] isEqualToString:#"userlike"])
{
NSString *userName = [[rq URL] host];
// extract username from the URL, then
return NO;
}
// else
return YES;
}
EDIT: I found this, exactly what you're looking for. (as it turns out, #regan also suggested the exact same thing.)
You can call selectors from the HTML code, see for example here and here.

Binding data to scroll view

I am new to iphone dev. I have a Label and a scroll view. I am trying to get data from url and bind it to label and a scroll view. I am able to bind the data to label by using...
UrlValues *asana=[[data yoga]objectAtIndex:i];
self.AsanaName.text = [asana asanatitle];
Similarly i want to bind some other data in the url to a scroll view. The data i want to bind is 4 or 5 sentences. So, can anyone help me how to do it... Any sample code will be really helpful... Thanks
if you want to show text from the URL fetch text from URL -
NSString *data_from_URL = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"urlgoes here"] encoding:NSUTF8StringEncoding error:nil];
Then you can set this text in the UITextView -
UITextView *sv = [[UITextView alloc] init];
[sv setText:data_from_URL];

Just how to you use TTStyledTextLabel?

All I want is to display some simple text in my viewController and have the hyperlinks autoparsed. When the user clicks on the link I want the control to somehow do a callback where I can do something with the URL. How can I achieve this?
I've already looked through the TTCatalog for hours. I have also tried looking into the source code of three20 as well as looking at the stack trace. No help. I just can't figure out how my app can react to the click of the URL. Any hints please?
Hard to help without seeing what you've already tried, but you should be able to do something like the following:
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc]
initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
You can then use TTNavigator and TTURLMap to map custom-uri://some/url to a particular controller in your application, or handle it yourself in your application delegate. The best place to find out how to do that is by looking at the TTNavigatorDemo sample application included in the Three20 source. Specifically, look at AppDelegate.m which is where all the URL mapping gets performed.
In addition to what Nathan says about URL mapping and links, you can also use CSS styles!
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work and
<span class=\"redText\">this should be red</span>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
Then in your StyleSheet.m implement
- (TTStyle*) redText {
return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] color:RGBCOLOR(255,0,0) next:nil];
}