Bullet points are not recognized in HTML String - ios

I have a string:
NSString *str1 = #"\u2022 You were held in custody for a longer
period of time than may have been necessary.";
I am converting it into an HTML string using this code :
- (NSString *)HTMLString {
NSDictionary * const exportParams = #{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:str1];
NSData *htmlData = [attributed dataFromRange:NSMakeRange(0, attributed.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}
But bullet points are not showing in the HTML string. It's showing a ? instead of bullet point. Please tell me any solution.

Please try below code -
- (NSString *)HTMLString {
NSDictionary * const exportParams = #{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
letterString = [letterString stringByReplacingOccurrencesOfString:#"\u2022" withString:#"•"];
NSAttributedString *attributed = [[NSAttributedString alloc] initWithString:letterString];
NSData *htmlData = [attributed dataFromRange:NSMakeRange(0, attributed.length) documentAttributes:exportParams error:nil];
return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}

Related

Remove html tags and display in UILabel Objective c

I am getting &nbsp /br> p> like Html tags in my api response. I want to display those contents in UILabel
What I did is:
NSString *STR_api = [NSString StingWithFormat#:"%#",[API_res valueforkey:#"description"]];
STR_api = [STR_api StringByreplacingaccurancesofString #"&nbsp" with string#""];
What I want is, In web portal it is displaying like bold, paragraph is it possible to display in UILabel by formatting the above response
Thanks in advance
I tried from perfect solution from Larme's answer
I got the solution.It works fine.
NSString *strHTML = #"S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>";
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:#(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
NSLog(#"html: %#",strHTML);
NSLog(#"attr: %#", attrStr);
NSLog(#"string: %#", [attrStr string]);
NSString *finalString = [attrStr string];
NSLog(#"The finalString is - %#",finalString);
The printed results are
html
html: S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>
string
string: S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi
Final String
The finalString is - S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi
Now for removing &nbsp from string
OPTION 1
NSRange range;
while ((range = [finalString rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
finalString = [finalString stringByReplacingCharactersInRange:range withString:#""];
OPTION 2
finalString = [finalString stringByReplacingOccurrencesOfString:#"&nbsp" withString:#""];
Remove HTML Tags from String
try this code:
NSString *strDescription = [NSString stringWithFormat:#"%#",[API_res valueforkey:#"description"]];
self.lblDescription.attributedText = [self getData:strDescription];
Convert HtmlString to string
-(NSAttributedString *)getData:(NSString *)str
{
NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = #{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
NSAttributedString *decodedString;
decodedString = [[NSAttributedString alloc] initWithData:stringData
options:options
documentAttributes:NULL
error:NULL];
return decodedString;
}
Use below UILabel formatting control which provides Rich text formatting based on HTML-like markups for iOS.
RTLabel

Saving NSAttributedString to Parse.com

Parse doesn't support direct saving of NSAttributedStrings. Converting to HTML isn't the most straightforward. Anyone have a friendly method for storing NSAttributedStrings (font & superscript) to Parse.com?
Thanks guys, decided to go with saving an rtf string onto Parse, based off #Wain's comment.
// convert NSAttributedString to RTFString and save to Parse
PFObject *note = [PFObject objectWithClassName:#"Note"];
NSAttributedString *noteAttributedText = self.noteTextView.attributedText;
NSDictionary *documentAttributes = [NSDictionary dictionaryWithObjectsAndKeys:NSRTFTextDocumentType,NSDocumentTypeDocumentAttribute, nil];
NSData *rtfData = [noteAttributedText dataFromRange:NSMakeRange(0, noteAttributedText.length) documentAttributes:documentAttributes error:NULL];
NSString *rtfString = [[NSString alloc] initWithData:rtfData encoding:NSUTF8StringEncoding];
note[#"noteTextAsRTFString"] = rtfString;
// convert RTFString to NSAttributedString after pulling from Parse
NSString *rtfString = [pfObject objectForKey:#"noteTextAsRTFString"];
NSData *data = [rtfString dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *noteAttributedText = [[NSAttributedString alloc] initWithData:data options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];

NSString iOS Russian encoding

I'm using code below:
AVMetadataItem *item = [self.player.metaData objectAtIndex:0];
NSLog("%#", item.stringValue);
Its works good with any english song title.
But, when i'm getting russian song title from AVMetadataItem:
ÐÐÐÐРÐЯ - ÐРСÐРÐÐÐТÐÐУ // СÐУШÐЮТ: 1585
How can i get something like:
Тратата - мы везем с собой кота.
Any help appreciated.
Try this:
NSData *test = [item.stringValue dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
We get meta from audio in UTF8 and don't know in what NSStringEncoding it's converted so i use:
for (i = 0; i < 15; i++) {
NSData *test = [item.stringValue dataUsingEncoding:i allowLossyConversion:YES];
NSString *dataString = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
}
Looks like iOS encoding Cyrillic(UTF8) in ISOLatin.

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!

String printing incorrectly in table view after JSON parsing

I one of my apps, i parse some data from local host and print it in a table view. To get the data, the user first logs in using an alert view. The user id entered is then used to fetch the data which i parse using JSON.
There is definitely a very simple solution to this question but I can't seem to be able to fix it. The problem is that when I print the data the string comes in this format:
( "string" )
But I want it so that it just says : string
in the table view. Here is my parsing method:
- (void)updateMyBooks
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Fetch data on a background thread:
NSString *authFormatString =
#"http://localhost:8888/Jineel_lib/bookBorrowed.php?uid=%#";
NSString *string = [[NSString alloc]initWithFormat:#"%#",UserID];
NSString *urlString = [NSString stringWithFormat:authFormatString, string];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"uel is %#", url);
NSString *contents = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
response1 = [contents JSONValue];
if (contents) {
// ... Parse JSON response and add objects to newBooksBorrowed ...
BookName = [[NSString alloc]init];
DateBorrowed = [[NSString alloc]init];
BookID = [[NSString alloc]init];
BookExtended = [[NSString alloc]init];
BookReturned = [[NSString alloc]init];
BookName = [response1 valueForKey:#"BookName"];
BookID = [response1 valueForKey:#"BookID"];
DateBorrowed = [response1 valueForKey:#"DateBorrowed"];
BookExtended = [response1 valueForKey:#"Extended"];
BookReturned = [response1 valueForKey:#"Returned"];
dispatch_sync(dispatch_get_main_queue(), ^{
// Update data source array and reload table view.
[BooksBorrowed addObject:BookName];
NSLog(#"bookBorrowed array = %#",BooksBorrowed);
[self.tableView reloadData];
});
}
});
}
This is how I print it in the table view:
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
When I use log during the parsing process, it comes out as ( "string" ) so the problem is somewhere in the parsing, at least thats what I think.
If
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
returns something like "( string )" then the most probably reason is that
[BooksBorrowed objectAtIndex:indexPath.row]
is not a string, but an array containing a string. In that case,
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];
should be the solution.
NSString *string = [[NSString alloc] initWithFormat:#"%#",[BooksBorrowed objectAtIndex:indexPath.row]];
string = [string stringByReplacingOccurrencesOfString:#"(" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#")" withString:#""];
string = [string stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSLog(#"string is %#",string);
cell.textLabel.text = string;
EDIT:
Above code is used if it's showing text in that format in your label.
If you see that in NSlog then it's NSString inside NSArray.
You need to fetch that string first from array and then display, use code line suggested by #Martin R for that.
NSString *string = [[BooksBorrowed objectAtIndex:indexPath.row] objectAtIndex:0];

Resources