iOS clickable text inside UITextView - ios

Is there a way to make clickable parts of UITextView. Actually I want to make text something like
By clicking “Register” above, you are agreeing to the Terms of Services and Privacy Statement
where Terms of Services should be one link and Privacy Statement another. And by clicking on those I should do something.

I did it with the code above using this project
- (void)_configureTermsLabel
{
self.termsOfUseLabel.hidden = YES;
self.termsAndConditionsLabel = [[TTTAttributedLabel alloc] initWithFrame:self.termsOfUseLabel.frame];
self.termsAndConditionsLabel.font = [UIFont systemFontOfSize:14];
self.termsAndConditionsLabel.lineBreakMode = UILineBreakModeWordWrap;
self.termsAndConditionsLabel.numberOfLines = 0;
NSString *termsStr = NSLocalizedString(#"Terms of use", #"Terms of use");
NSString *privacyStr = NSLocalizedString(#"Privacy Policy", #"Privacy Policy");
NSString *andStr = NSLocalizedString(#"and", #"and");
NSString *conductStr = NSLocalizedString(#"Code of conduct", #"Code of conduct");
NSString *termsAndConditionsStr = [NSString stringWithFormat:#"%# - %# %# %#", termsStr,
privacyStr, andStr, conductStr];
self.termsAndConditionsLabel.text = termsAndConditionsStr;
NSString *languageCode = [[GLQAppDelegate sharedDelegate] languageIdentifier];
NSURL *termsURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQTermsOfUseURL, languageCode]];
NSURL *privacyURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQPrivacyPolicyURL, languageCode]];
NSURL *conductURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQCodeOfConductURL, languageCode]];
NSRange termsRange = [self.termsAndConditionsLabel.text rangeOfString:termsStr];
NSRange privacyRange = [self.termsAndConditionsLabel.text rangeOfString:privacyStr];
NSRange conductRange = [self.termsAndConditionsLabel.text rangeOfString:conductStr];
[self.termsAndConditionsLabel addLinkToURL:termsURL withRange:termsRange];
[self.termsAndConditionsLabel addLinkToURL:privacyURL withRange:privacyRange];
[self.termsAndConditionsLabel addLinkToURL:conductURL withRange:conductRange];
self.termsAndConditionsLabel.delegate = self;
self.termsAndConditionsLabel.userInteractionEnabled = YES;
[self.scrollView addSubview:self.termsAndConditionsLabel];
}

Related

Write Google sheet cell

I try to find how modify or write a cell in a google sheet.
I success to read my sheet (on my drive) with quickstart guide (I had copied and pasted this code : https://developers.google.com/sheets/quickstart/ios#step_3_set_up_the_sample). I had just changed the url by :
https://sheets.googleapis.com/v4/spreadsheets/my_spreadsheet_Id/values/Feuil1!A1:F
.
But impossible to find a code to write on cells of my sheet... when i look : https://developers.google.com/sheets/guides/values#methods. I don't understand where i should put my new data to the cell.
Exemple : i have "New York" on the cell A1.
i want to change "New York" by "Tahiti".
Do you know how do that ?
i tried this but not working :
- (void)modifyListe {
NSString *baseUrl = #"https://sheets.googleapis.com/v4/spreadsheets/";
NSString *spreadsheetId = #"{MySpredsheet_ID}"; // choisir la bonne
NSString *range = #"/values/Feuil1!G1:G1?valueInputOption=Tahiti";
baseUrl = [baseUrl stringByAppendingString:spreadsheetId];
baseUrl = [baseUrl stringByAppendingString:range];
[self.service fetchObjectWithURL:[NSURL URLWithString:baseUrl]
objectClass:[GTLObject class]
delegate:self
didFinishSelector:#selector(displayMajorsWithServiceTicketT:finishedWithObject:error:)];
}
SOLUTION : Look second post
I think found the solution (inspired by this post) :
NSString *baseUrl = #"https://sheets.googleapis.com/v4/spreadsheets/MyspreadsheetID/values/Donnees!G1:G1?valueInputOption=USER_ENTERED";
NSURL *theURL = [NSURL URLWithString:baseUrl];
NSString *rangeKEY = #"range";
NSString *dimensionKEY = #"majorDimension";
NSMutableString *valuesKEY = [NSMutableString stringWithString:#"values"];
NSString *therange = #"Donnees!G1:G1";
NSString *themajorDimension = #"ROWS";
NSMutableString *string_Value = [NSMutableString stringWithString:#"theValue"];
NSMutableArray *ArrayOfString = [NSMutableArray array];
NSMutableArray *arrayOfArray = [NSMutableArray array];
[ArrayOfString addObject:string_Value];
[arrayOfArray addObject:ArrayOfString];
NSMutableDictionary *dicooo = [NSMutableDictionary dictionary];
[dicooo setObject:arrayOfArray forKey:valuesKEY];
[dicooo setObject:therange forKey:rangeKEY];
[dicooo setObject:themajorDimension forKey:dimensionKEY];
GTLObject *theobject ;
theobject = [GTLObject objectWithJSON:dicooo];
[self.service fetchObjectByUpdatingObject:theobject forURL:theURL delegate:self didFinishSelector:#selector(displayMajorsWithServiceTicketT:finishedWithObject:error:)];
When I launch I can see the modification on my sheet.

How to get highlighted text range in WebView Content in iOS?

Applied this code in .js file
var selectedText = "";
function getHighlightedString()
{
var text = document.getSelection();
startIndex = text.anchorOffset;
endIndex = text.focusOffset;
selectedText = text.anchorNode.textContent.substr(startIndex,endIndex - text.anchorOffset);
var rangeText = document.getSelection().toString();
}
function highlight() {
if (typeof window.getSelection != "undefined") {
var range = document.getSelection().getRangeAt(0);
var selectionContents = range.extractContents();
var span = document.createElement("span");
span.appendChild(selectionContents);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor = "rgb(237,191,245)";
span.style.color = "orange";
range.insertNode(span);
}
}
And I'm using this code for highlighting the text.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"first" ofType:#"js" inDirectory:#""];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[webView_Detail stringByEvaluatingJavaScriptFromString:jsString];
NSString *highlightFunction = [NSString stringWithFormat:#"highlight()"];
[webView_Detail stringByEvaluatingJavaScriptFromString:highlightFunction];
highlightedString = [webView_Detail stringByEvaluatingJavaScriptFromString:#"document.documentElement.outerHTML"];
I stuck for getting the text range. it i have to send to server.but i couldn't able to get selected text range.
Please help me.
Try Following
NSString * strJS = #"window.getSelection().toString()";
NSString * strSelectedText = [self.webView stringByEvaluatingJavaScriptFromString:strJS];
NSString *plainText = [self.webView stringByEvaluatingJavaScriptFromString:#"document.body.textContent"];
NSRange * rangeOfString = [plainText rangeOfString:strSelectedText];
You can try following alternate also
NSString *plainText = [self.webView stringByEvaluatingJavaScriptFromString: #"document.body.innerText"];
If there are multiple matches found for selected text, please refer following link to find all ranges of occurrence.
Find all locations of substring in NSString (not just first)

Send HTML email with SKPSMTP iOS

I am trying to send an HTML email from my SKPSMTP code in iOS. Right now, I'm just sending plain text, but I'm trying to upgrade that a little. I've included that code below.
I can't find any documentation. How can I upload an HTML file and include that as it's body. Also, there's an image that's being loaded from the same directory as the HTML file, if that makes a difference in the answer. Thanks.
NSMutableString *emailBody = [NSMutableString stringWithFormat:#"Here's your code again, "];
[emailBody appendString:userCode];
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = #"me#gmail.com";
NSString *toEmail = [NSString stringWithFormat:#"%#", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = #"smtp.gmail.com";
email.requiresAuth = YES;
email.login = #"me#gmail.com";
email.pass = #"myPass";
email.subject = #"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
emailBody,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:plainPart, nil];
// Send it!
[email send];
So, here's the answer I came across, just so everyone else can get the benefit of me struggling through:
//Send them an e-mail
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: #"loginEmail" ofType: #"html"];
NSString *result = [NSString stringWithContentsOfFile: path encoding:
NSUTF8StringEncoding error: &error];
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:#"<!--INJECT CODE HERE -->"
options:0
error:&error];
NSString *emailBody = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:code];
NSLog(#"%#", [emailBody class]);
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = #"myemail#gmail.com";
NSString *toEmail = [NSString stringWithFormat:#"%#", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = #"smtp.gmail.com";
email.requiresAuth = YES;
email.login = #"myemail#gmail.com";
email.pass = #"myPass"
email.subject = #"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *htmlPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/html",kSKPSMTPPartContentTypeKey, emailBody,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:htmlPart, nil];
// Send it!
NSLog(#"ABOUT TO SEND");
[email send];
So, I had to write an HTML file, host all my images on tinypic to include in the HTML, write some text to regex switch out my code variable, load in it in here and attach it as the part of my email with key "text/html". This code works, but if anyone has any other suggestions that are helpful, I'm willing to mark them as the right answer!

A better approach to coding the a string value test for a NSDictionary object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How could I write this if else code checking in a better way?
My conditional code here seems repetitive and long. Is there a better approach? I want to test for a string value in a NSDictionary object and then depending upon the value prefix a UILabel with $, £, ¥ currency symbols.
Here's my code (I've just shown 2 examples below, I have more currencies and the code is very long):
if ([[item objectForKey:#"currency"] isEqualToString:#"EUR"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"GBP"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"€%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
if ([[item objectForKey:#"currency"] isEqualToString:#"USD"]) {
NSString *priceConvertToStr = [NSString stringWithFormat:#"$%#", [[item objectForKey:#"price"]stringValue]];
NSString *priceStringFix = [priceConvertToStr
stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];
}
thanks so much for any help.
You can refactor your code to this (possible because essentially 80% of the code within the if statements are identical):
NSDictionary *currDict = #{
#"EUR": #"€",
#"GBP": #"₤",
#"USD": #"$"
};
NSString *currName = [item objectForKey:#"currency"];
NSString *currency = [currDict objectForKey:currName];
NSString *priceConvertToStr = [NSString stringWithFormat:#"%#%#",
currency,
[[item objectForKey:#"price"] stringValue]
];
NSString *priceStringFix = [priceConvertToStr stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
priceLabelText.text = priceStringFix;
[imgView2 addSubview:priceLabelText];

MKPinAnnotationView loading all data in the didSelectAnnotationView method at once

I have been poking around with the MKMapView for quite some time trying to get more familiar with it and I ran into a problem.
I have my MapView populated with two pins and when I pressed them they have their respective annotations. I also have a button that will take me to a new UIView with UlLabels that will be loaded with data from an array.
Looking at my console I notice that the data is coming through but instead of it being just for the pin I selected its loading all of them and then displaying the last one.
Here is my code for the method I'm using to load the data for the next view:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:addAnnotation reuseIdentifier:#"currentloc"];
if(annView.tag = 0) {
GSStore * theStore = [globalCMS getStoreById:1];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:#"selectedstore"];
NSLog(#"%#", theStore.name);
storeName.text = [NSString stringWithFormat:#"%#", theStore.name];
storeCS.text = [NSString stringWithFormat:#"%#,%#", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:#"%#", theStore.hours];
storePhone.text = [NSString stringWithFormat:#"%#", theStore.phone];
storeAddress.text = [NSString stringWithFormat:#"%#", theStore.address];
storeCSZ.text = [NSString stringWithFormat:#"%#,%# %#", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:#"%#", theStore.website];
}
if(annView.tag = 1){
GSStore * theStore = [globalCMS getStoreById:2];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:#"selectedstore"];
NSLog(#"%#", theStore.name);
storeName.text = [NSString stringWithFormat:#"%#", theStore.name];
storeCS.text = [NSString stringWithFormat:#"%#,%#", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:#"%#", theStore.hours];
storePhone.text = [NSString stringWithFormat:#"%#", theStore.phone];
storeAddress.text = [NSString stringWithFormat:#"%#", theStore.address];
storeCSZ.text = [NSString stringWithFormat:#"%#,%# %#", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:#"%#", theStore.website];
}
if (annView.tag = 2) {
GSStore * theStore = [globalCMS getStoreById:3];
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int storeid = theStore.storeid;
[prefs setInteger:storeid forKey:#"selectedstore"];
NSLog(#"%#", theStore.name);
storeName.text = [NSString stringWithFormat:#"%#", theStore.name];
storeCS.text = [NSString stringWithFormat:#"%#,%#", theStore.city, theStore.state];
storeHours.text = [NSString stringWithFormat:#"%#", theStore.hours];
storePhone.text = [NSString stringWithFormat:#"%#", theStore.phone];
storeAddress.text = [NSString stringWithFormat:#"%#", theStore.address];
storeCSZ.text = [NSString stringWithFormat:#"%#,%# %#", theStore.city, theStore.state, theStore.zip];
storeWebsite.text = [NSString stringWithFormat:#"%#", theStore.website];
}
}
Just to explain why you are seeing "the method loading all the data":
The if conditions are using a single equals (=) sign instead of a double (==).
The single = is an assignment while the double == is the one for checking equality.
Since the assignment executes successfully in all the ifs, the code inside all the ifs executes.
However, the real problem is that you are creating a new instance of an annotation view in the didSelectAnnotationView delegate method which is not what you want.
Instead of creating a new instance, you can use the annotation view instance that the method is providing you as a parameter (ie. view).
So theoretically you could look at view.tag.
But I would highly recommend not relying on tags and instead placing all the data you need for an annotation in the annotation class itself. Then you can access the annotation in this method using view.annotation and casting it to your custom class to access custom properties:
MyAnnotationClass *myAnn = (MyAnnotationClass *)view.annotation;
NSLog(#"myAnn.someCustomProperty = %#", myAnn.someCustomProperty);

Resources