Code crashing during initialization of NSMutableAttributedString - ios

I need to initialize NsmutableAttributedString with a string productDesc, but the code crashes in the line
attrStrInfoLabel= [[NSMutableAttributedString alloc] initWithString:productDesc];
with error [NSConcreteMutableAttributedString _encodingCantBeStoredInEightBitCFString].
Please advice my code is
NSMutableAttributedString *attrStrInfoLabel;
NSMutableString *productDesc;
productDesc = [NSMutableString stringWithFormat:#"PRODUCT DESCRIPTION:%#", [productDescription objectAtIndex:i]];
attrStrInfoLabel= [[NSMutableAttributedString alloc] initWithString:productDesc];

Try out using NSAttributedString instead of NSMutableString.
Take a look at below Code Sample.
NSMutableAttributedString *attrStrInfoLabel = [[NSMutableAttributedString alloc] init];
NSAttributedString *productDesc = [[NSAttributedString alloc] initWithString:[NSMutableString stringWithFormat:#"PRODUCT DESCRIPTION:%#",[productDescription objectAtIndex:i]];
[attrStrInfoLabel appendAttributedString:productDesc];
Meanwhile check the data in productDescription too. Put some check whether is !nil and has count>0.

The code you posted does not cause the error, but I am going to assume you have an NSLog() call in the following line:
NSLog(#"%#", attrStrInfoLabel);
which should be changed to:
NSLog(#"%#", [attrStrInfoLabel string]);

Initialise it and then append.
NSMutableString *str = [[NSMutableString alloc] init];
[str appendString:YOUR_STRING];

Related

Display Emoji from unicode in UILabel in iOS

I am working on emoji in chat app.
When someone send me emoji in message it look like this type :- Hello...(worried) how are you(happy)?. Here (worried)and (happy) are assigned keys for emojis.
This is list for emojis and key and value.
dictemoji = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"worried-48.gif",#"(worried)",
#"sad-48.gif",#"(sad)",
#"bandit48.gif",#"(bandit)",
#"wink48.gif",#"(wink)",
#"surprised48.gif",#"(surprised)",
#"smirking48.gif",#"(smirking)",
#"laugh48.gif",#"(laugh)",
#"cool48.gif",#"(cool)",
#"stoned-48.gif",#"(stoned)",
#"smile-48.gif",#"(smile)",
#"nerd-48.gif",#"(nerd)",
#"happy-48.gif",#"(happy)",
#"evil-grin-48.gif",#"(evil-grin)",
#"tongue48.gif",#"(tongue)",
#"lips-sealed-48.gif",#"(lips-sealed)",
#"GIF48.gif",#"(GIF)",
#"dull48.gif",#"(dull)",
nil];
When I received message Hello...(worried) how are you(happy)? I want to saw my emoji instead of (worried)and(happy) in label.
So how can I take emoji instead of those words?
EDIT:-
When someone send me emoji with text message, it will replace with dictionary value :
for (NSString *emojiKey in dictemoji.allKeys)
{
if ([message containsString:emojiKey])
{
message = [message stringByReplacingOccurrencesOfString:emojiKey withString:[dictemoji valueForKey:emojiKey]];
}
}
// helllo(sad)...how are you(smile)...? ----->it will look like helllo(sad-48.gif)...how are you(smile-48.gif)...?
NSLog(#"message updated:%#",message);
cell.textLabel.text=message;
So, I want to display emoji where (sad-48.gif) and (smile-48.gif) printed in label.
Try this. It may help you.
Add one label & write the following code in viewDidLoad :
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
_lbl.text = valueEmoj;
You have to use like this ;
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:[dictemoji valueForKey:emojiKey]];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:#"My label text"];
[myString appendAttributedString:attachmentString];
cell.attributedText = myString;
hope this will help.

Attributted String link not opening

I need to set a couple of links on the click of an attributed string in a label/textview(UILabel is preferred). I used the following code for setting the links.(multiple links in same label).
NSMutableAttributedString *firstAttributedString = [[NSMutableAttributedString alloc] initWithString:#"By tapping 'Yes', you agree to the " ];
NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc] initWithString:#"Terms of Use "];
[firstAttributedString appendAttributedString:secondAttributedString];
NSMutableAttributedString *thirdAttributedString = [[NSMutableAttributedString alloc] initWithString:#"and "];
[firstAttributedString appendAttributedString:thirdAttributedString];
NSMutableAttributedString *fourthAttributedString = [[NSMutableAttributedString alloc] initWithString:#"Privacy Policy "];
[firstAttributedString appendAttributedString:fourthAttributedString];
NSMutableAttributedString *fifthAttributedString = [[NSMutableAttributedString alloc] initWithString:#"of example"];
[firstAttributedString appendAttributedString:fifthAttributedString];
NSURL *URL = [NSURL URLWithString: #"example://termsOfUse"];
[secondAttributedString addAttribute: NSLinkAttributeName value: URL range: NSMakeRange(0, secondAttributedString.length)];
[alertTextView setUserInteractionEnabled: YES];
alertTextView.editable=NO;
alertTextView.attributedText = firstAttributedString;
alertTextView.dataDetectorTypes = UIDataDetectorTypeLink;
I have set my info.plist to respond to the url scheme call, but the method in the
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
delegate is not triggering. So I am assuming the click is not getting triggered. What am I doing wrong here? What can I do to solve this?
Upon further review of your sample code, you are trying to modify secondAttributedString after it has been appended to firstAttributedString. This will not produce the results you want, as NSAttributedStrings are not like Arrays.
Think of appending strings like appending a copy of whatever you pass in.
Try adding the link attributes before appending secondAttributedString to the first one.
NSMutableAttributedString *firstAttributedString = [[NSMutableAttributedString alloc] initWithString:#"By tapping 'Yes', you agree to the " ];
NSMutableAttributedString *secondAttributedString = [[NSMutableAttributedString alloc] initWithString:#"Terms of Use "];
NSURL *URL = [NSURL URLWithString: #"example://termsOfUse"];
[secondAttributedString addAttribute: NSLinkAttributeName value: URL range: NSMakeRange(0, secondAttributedString.length)];
[firstAttributedString appendAttributedString:secondAttributedString];
NSMutableAttributedString *thirdAttributedString = [[NSMutableAttributedString alloc] initWithString:#"and "];
[firstAttributedString appendAttributedString:thirdAttributedString];
NSMutableAttributedString *fourthAttributedString = [[NSMutableAttributedString alloc] initWithString:#"Privacy Policy "];
[firstAttributedString appendAttributedString:fourthAttributedString];
NSMutableAttributedString *fifthAttributedString = [[NSMutableAttributedString alloc] initWithString:#"of example"];
[firstAttributedString appendAttributedString:fifthAttributedString];
[alertTextView setUserInteractionEnabled: YES];
alertTextView.editable=NO;
alertTextView.attributedText = firstAttributedString;
Make sure your UITextView has the following properties set:
textView.editable = NO;
textView.dataDetectorTypes = UIDataDetectorTypeAll;
textView.delegate = self; // assuming `self` is the ViewController
You should instead implement the following delegate method:
textView:shouldInteractWithURL:inRange:
and return NO and handle the action yourself.

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."]];

Multiple text alignment with same label using NSMutableAttributedString on iOS

I want to set Text direction for my label using NSMutableAttributedString. for example, I have chat view, which contains message and time. I want to set left alignment for message and right alignment for time using UILabel.
I have used following code, but it's not working,
NSString *Time = [Functions stringFromGivenDate:msg.time withFormate:#"hh:mm a"];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#",msg.text,Time]];
NSDictionary *attrDictionary = #{NSWritingDirectionAttributeName:#[#(NSTextWritingDirectionOverride)]};
[str addAttributes:attrDictionary range:NSMakeRange(msg.text.length+1, Time.length)];
if I understood correctly, this should help:
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = alignment;// type NSTextAlignment
NSDictionary *attributtes = #{NSParagraphStyleAttributeName : style,};

Separate NSMutablestring with comma [duplicate]

This question already has answers here:
How to append a NSMutableString with a NSMutableString?
(4 answers)
Closed 9 years ago.
i'm trying to append two NSMutablestring, but i want to make the comma separation between them. Right now the code i use is this:
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
NSLog(#"%#",teststring);
the result is :This is a stringThis is a String
i try with:
[teststring appendFormat:#",%#",astring];
but still no luck.
Please help, regards.
teststring = [[NSMutableString alloc]init];
[teststring appendString:#","];
[teststring appendString:astring];
Try this
NSString *astring = #"This is a string";
teststring = [[NSMutableString alloc]init];
[teststring appendString:astring];
[teststring appendString:#","];
[teststring appendString:astring];
NSLog(#"%#",teststring);
Try this
NSString *string = #"Hello World";
NSMutableString * secondstring =[[NSMutableString alloc]init];
[secondstring appendString:string]
[secondstring appendString:#","];
[secondstring appendString:string];
If you want to see this String
NSLog(#"%#",secondstring);
I used this and its working fine for me :
NSMutableString *teststring = [[NSMutableString alloc]init];
NSMutableString *astring = [[NSMutableString alloc]init];
[astring appendString:#"I am appending this string"];
[teststring appendFormat:#",%#",astring];
NSLog(#"string : %#",teststring);
O/P : string : ,I am appending this string

Resources