How to show HTML text from API in Objective C? - ios

I Am getting a string from API which is "http://www.maximusautogroup.com/\" target=\"_blank\">here". From this string I have to show "here" as an hyperlink of that link. i am not getting how to do that.

Try this:
NSString *FileName = [[Url lastPathComponent] stringByDeletingPathExtension];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:FileName];
[attributeString addAttributes:#{NSStrikethroughStyleAttributeName: #(NSUnderlineStyleSingle)
, NSBackgroundColorAttributeName: [UIColor blueColor]} range:NSMakeRange(0, [attributeString length])]
[_buyButton setAttributedTitle:attributeString forState:UIControlStateNormal];

Related

iOS: how to change colour/font of some characters of a string

in my app i have to change the font of a part of string which comes from JSON response
> "<span class=\"drop_data_user\">Andrew James</span> liked your comment
> \"hiiiiiiiiiiiiiii\" that you posted."
to convert it in attributed string i am using the following code
NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[NotificationTxt dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:#(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
I want to change the font colour of the sender
Also, you can use it as follows.
NSString *dateString = [NSString stringWithFormat:#"%#%#", #"Teslimat: ", selectedReservationModel.DateLabel];
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString: dateString];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansBoldFontSize:12] range:NSMakeRange(0, 8)];
[attrS addAttribute:NSFontAttributeName value:[GenericUtility getOpenSansRegularFontSize:12] range:NSMakeRange(9, selectedReservationModel.DateLabel.length)];
lblDeliveryDate.attributedText = attrS;
NSMutableAttributedString *attrS = [[NSMutableAttributedString alloc] initWithString:#"My String"];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[attrS addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3, 6)];
self.myLabel.attributedText = attrS;
For fonts use
[attrS addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
I believe the easiest way to fulfil your task is to use CSS inlined in the text, then parsing HTML as you do will change the color too (I'm using Swift syntax, but I believe you can easily convert it to ObjC):
let text = "<span class=\"drop_data_user\">Andrew James</span> liked your comment \"hiiiiiiiiiiiiiii\" that you posted."
let colouredAuthorText = "\(text)<style>.drop_data_user { color: #FEB600; }</style>"
Then just parse colouredAuthorText (which is created by appending style to the original text) instead of the original, and you should be good to go.

Bolding an nsstring

I have looked online for ways to bold a NSString and either it's not there or I can't seem to find it. I simply want to bold the word in the NSString but so far, what I have done is not woking:
NSString *player1Name = [[UIFont boldSystemFontOfSize:12] fontName];
I have relied on this to make "player1Name" bold but it doesn't work at all.
When I run it I get this:
Thanks in advance to anyone who helps me figure out the solution.
You can not bold a NSString... Try a NSAttributedString...
NSAttributedString *player1Name = [[NSAttributedString alloc] initWithString:#"name" attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]}];
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/
fontName is a property on UIFont
[[UIFont boldSystemFontOfSize:12] fontName]
returns the font name HelveticaNeueInterface-MediumP4
Try this:
NSString *myString = #"My string!";
NSAttributedString *myBoldString = [[NSAttributedString alloc] initWithString:myString
attributes:#{ NSFontAttributeName: [UIFont boldSystemFontOfSize:35.0] }];
Also you can set range for particular text like:
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(2, 4);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
You can't make NSString bold. Instead of that you can make UILabel text into bold. Because UILabels are for UI representation & NSStrings are just objects. So make your UILabel bold
[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

Add a nsstring at the end of attributed string in textview iOS

I have a text like "Hello There" which will be a attributed string with red color. Then at the end of that attributed string I want to append a nsstring 'Who are you?' But whenever I append a nsstring, the whole string becomes normal nsstring and the property of the attributed string is being removed. My attempt so far:
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Hello There"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,[attributeString length])];
NSMutableAttributedString *previousAttrString = [[NSMutableAttributedString alloc] initWithString:messageTextView.text];
[previousAttrString insertAttributedString: attributeString atIndex:location];
messageTextView.attributedText = previousAttrString;
messageTextView.font = [UIFont fontWithName:#"Helvetica" size:15];
NSString *messageWithContact = [NSString stringWithFormat: #"%# %#", messageTextView.text, #"Who are you?"];
messageTextView.text=messageWithContact;
What I did wrong? Please help me.
Replace the bottom lines
NSString *messageWithContact = [NSString stringWithFormat: #"%# %#", messageTextView.text, #"Who are you?"];
messageTextView.text=messageWithContact;
with
NSMutableAttributedString *newString = messageTextView.attibutedText;
[newString appendAttributedString: [[NSAttributedString alloc] initWithString: #"Who are you?"];
messageTextView.attibutedText = newString;

String and attributions problems

Ive been messing around with this without proper results. My code is as follows
NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc]initWithString:((User *)appDelegate.users[self.currentUserIndex]).name];
[nameString addAttribute:NSFontAttributeName value:[UIFont fontWithName:#"Helvetica-Bold" size:12.0] range:NSMakeRange(0, nameString.length)];
NSString *adviceString = [[NSString alloc] initWithFormat:#"%#, remember be extra aware of the \n cat today. The dog index is 4, dogcatcher \n suggests you should at minimum wear \n a dog and apply cat...", [nameString string]];
So, it doesn't bold the username as desired. I tried using NSAttributedString for adviceString but then I can't use the initWithFormat: method and list the variables after the string, and I don't see any NSAttributedString equivalent. I wanted to you NSAttributedString instead of NSMutableAttributedString, but it doesn't seem to recognise the addAttribute:value:fontWithName:range method. Can anybody help me out? Any help much appreciated.
That's for sure, you need to use NSAttributedString also for adviceString to get attributed.
The code goes like this, if you need to use stringWithFormat:
NSString *fullName = #"Anoop Kumar Vaidya";
NSMutableAttributedString *attributedName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"Hello %#", fullName]
attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]}];
In the attributes you can add few more desired/required attributes.
EDIT 2:
You may like to use some methods as :
-(NSMutableAttributedString *)normalString:(NSString *)string{
return [[NSMutableAttributedString alloc] initWithString:string
attributes:#{}];
}
-(NSMutableAttributedString *)boldString:(NSString *)string{
return [[NSMutableAttributedString alloc] initWithString:string
attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]
}];
}
And then use them as per your requirement:
NSMutableAttributedString *attributedName = [self boldString:fullName];
[attributedName appendAttributedString:[self normalString:#" is creating one"]];
[attributedName appendAttributedString:[self boldString:#" bold"]];
[attributedName appendAttributedString:[self normalString:#" string."]];

How to set multiple color text in table view in ios

I have a string ... Share Pictures .I want to show the Share in different color.I used NSMutableAttributedString to change the color of that part of the string.But when I am setting the cell.textLabel.text using the following string it doesn't work.Any other way to do this?
This way it doesn't work.
NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:#"Share pictures "];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
NSString *tempStr3 = #"with your friends.";
NSString *finalString3 = [NSString stringWithFormat:#"%#%#" , string3, tempStr3];
[menuTextArray addObject:finalString3];
And in table view datasource method.
cell.textLabel.text = [menuTextArray objectAtIndex:indexPath.row];
You need to add only NSMutableAttributedString into your menuTextArray:
NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc]initWithString:#"Share pictures with your friends"];
[yourString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
[menuTextArray addObject:yourString];
then set attributedText
cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
Use attributedText in place of text
cell.textLabel.attributedText = [menuTextArray objectAtIndex:indexPath.row];
Hope this code helps you.....
NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:#"Share pictures with your friends"];
[string3 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 5)];
[menuTextArray addObject:string3];
[cell.textLabel setAttributedText:string3];

Resources