How can I word wrap with NSMutableAttributedString? - objective-c

NSArray *myArray = #[#"1st:array1",
#"2nd:array2",
#"3rd:array3"
];
NSString *labelString = [myArray componentsJoinedByString:#"\n"];
In this codelabelStringcan be word wrapped.
But if use NSMutableAttributedString like this
NSAttributedString *resultString = [resultArray componentsJoinedByString:#"\n"];
it can't be joined by #"\n". Any other method is existed? Thanks.

It's not difficult. You just know one thing.
AttributedString can't have \n maybe. So you just put \n in NSString.
And just make NSAttributedString from this NSString.
Here this code. I hope this code help your work.
NSString *commentString;
NSMutableArray *resultArray = [[NSMutableArray alloc]initWithCapacity:50];
for (InstagramComment *comment in comments) {
commentString = [NSString stringWithFormat:#"%#:%#\n", comment.user.username, comment.text];
NSMutableAttributedString *styledCommentString = [[NSMutableAttributedString alloc]initWithString:commentString];
[resultArray addObject:styledCommentString];
}
NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc]init];
for (int i = 0; i < resultArray.count; ++i) {
[resultString appendAttributedString:[resultArray objectAtIndex:i]];
} [cell.comment setAttributedText:resultString];

Related

How to separate NSArray values?

NSArray has 3 values
#[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"]
And I want to put this array into the UILabel with word wrap.
So when I run, it should show like this in label.
addd:etyrhetwrwr
fdfdd:jjjjhhhh
fsuy:jjhwgggggg
But I can't separate this NSArray. How can I do that?
NSArray *myArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *labelString = [myArray componentsJoinedByString:#"\n"];
Then use the labelString to set your label text property.
Try This
NSArray *yourArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *string = [myArray componentsJoinedByString:#"\n"];
[yourLabel setText:string];
You can separate NSArray in UILabel by
NSArray myArr = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString strLabel = [myArr string:#"\n"];
try this code
NSArray *array = [[NSArray alloc] initWithObjects:#"adad",#"fgfdgdfg",#"sddgfs", nil];
NSMutableString *strFinalData = [[NSMutableString alloc] init];
for (int i=0; i<[array count]; i++)
{
[strFinalData appendFormat:#"%# ",[array objectAtIndex:i]];
}
NSLog(#"Final String: %#",strFinalData);
Now set yourLabel.text = strFinalData;

NSTextView making bold text using an array

I have an array which has the following structure:
(
[0] = (
[0] = #"Title string"
[1] = #"Some content string"
)
[1] = (
[0] = #"Title string"
[1] = #"Some content string"
)
[2] = (
[0] = #"Title string"
[1] = #"Some content string"
)
...
)
and so on and so fourth to a variating amount of reoccurrence.
My goal is to try and merge it all into one single string to display in an NSTextField, and make every title string bold. So the code above would look something like this if it were outputted.
Title String
Some content string
Title String
Some content string
Title String
Some content string
My first question is how could make a single string where certain text is bold; and my second question is how could I take that string and send it to the NSTextField?
So far this is what I've done.I've tried using NSMutableAttributedString to make the string like so:
NSMutableAttributedString *contentString = [NSMutableAttributedString alloc];
for (int i=0; i<[result count]; i++) {
[contentString addAttribute:NSFontAttributeName
value:[NSFont fontWithName:#"HelveticaNeue-Bold" size:16.0]
range:NSRangeFromString(result[i][0])];
}
But I couldn't figure out how to append anything else to the string, or try and print just that alone because I got this error when trying to the following to display it in the NSTextField.
[self->contentView setString:contentString];
Incompatible pointer types sending 'NSMutableAttributedString *' to paramater of type 'NSString *'
So I tried this instead, but got a different error
[self->contentView attributedString:contentString];
No visible #interface for 'NSTextView' declares the selector 'attributedString'
Managed to find a way to make it work
NSMutableAttributedString *contentString = [NSMutableAttributedString alloc];
for (int i=0; i<[result count]; i++) {
NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#\n\n", result[i][0], result[i][1]]];
NSDictionary *attributes = #{
NSFontAttributeName : [NSFont fontWithName:#"HelveticaNeue-Bold" size:12.0]
};
NSString *subtitle = [NSString stringWithFormat:#"%#", result[i][0]];
[resultString setAttributes:attributes range:NSMakeRange(0, [subtitle length])];
[contentString appendAttributedString:resultString];
}
[self->content setString:#""];
[[self->content textStorage] appendAttributedString:contentString];
The solution lies within the last line of code. All that needed to be done was instead of passing data to setString, use textStorage instead and pass an object to it. (I think I got the terminology right)
Bue yeah hope this helps anyone in the future!
How's this...
NSMutableAttributedString *contentString = [[NSMutableAttributedString alloc] init];
for (int i = 0; i < [result count]; i++) {
NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%#\n%#\n\n", result[i][0], result[i][1]]];
[resultString addAttribute:NSFontAttributeName
value:[NSFont fontWithName:#"HelveticaNeue-Bold" size:16.0]
range:NSRangeFromString(result[i][0])];
[contentString appendAttributedString:resultString];
}
Some notes about your other code. Make sure to match your [NSMutableAttributedString alloc] with an init like [[NSMutableAttributedString alloc] init]
You should realize that NSMutableAttributedString is not a subclass of NSString, but instead of NSObject, so setString: is not a method available to you.
NSTextView uses insertText: to set it's value.

How to combine 2 strings into one nsatributed string?

NSString * strTimeBefore = [timeBefore componentsJoinedByString:#" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:#" "];
I want the resulting string to be an NSAttributedString where the time in strTimeAfter is in bold
You probably want something like:
NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = [NSString stringWithFormat:#"%# %#", strTimeBefore, strTimeAfter;
// start at the end of strTimeBefore and go the length of strTimeAfter
NSRange boldedRange = NSMakeRange([strTimeBefore length] + 1, [strTimeAfter length]);
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];
[attrString beginEditing];
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:boldedRange];
[attrString endEditing];
And my answer is cribbed from Jacob's answer to this very closely related question.
Take two attribute string in that store your first string into one attribute string without changing its attributes, In second attribute string store your second string with changing its attibutes and then append both attribute string into one NSMutableAttributeString Try like this below:-
NSString * strTimeBefore = [timeBefore componentsJoinedByString:#" "];
NSString * strTimeAfter = [timeAfter componentsJoinedByString:#" "];
NSAttributedString *attrBeforeStr=[[NSAttributedString alloc]initWithString:strTimeBefore];
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:#"Arial" traits:NSBoldFontMask weight:5 size:14];
[attributes setObject:font forKey:NSFontAttributeName];
NSAttributedString *attrAftStr=[[NSAttributedString alloc]initWithString:strTimeAfter attributes:];
NSMutableAttributedString *string=[[NSMutableAttributedString alloc] init];
[string appendAttributedString:attrBeforeStr];
[string appendAttributedString:strTimeAfter];
Note: You can change font color as well in attribute string, if it is required.

How to create an NSarray with NSAttributedStrings but keeping the attributes within the array?

I want to store different strings with different attributes and store all of them in one array and then display the objects in one label but each object with its respective attribute.
Any suggestions?
EDIT: Solution derived from rmaddy's answer
NSDictionary *redAttrs = #{NSForegroundColorAttributeName:[UIColor redColor]};
NSDictionary *greenAttrs = #{NSForegroundColorAttributeName:[UIColor colorWithRed:0.118 green:0.506 blue:0.000 alpha:1.000]};
NSDictionary *orangeAttrs = #{NSForegroundColorAttributeName:[UIColor orangeColor]};
NSString *stringUm = #"Brazil";
NSString *stringDois = #"USA";
NSString *stringTres = #"England";
NSMutableAttributedString *redString = [[NSMutableAttributedString alloc] initWithString:stringUm];
[redString setAttributes:redAttrs range:NSMakeRange(0,4)];
NSMutableAttributedString *greenString = [[NSMutableAttributedString alloc] initWithString:stringDois];
[greenString setAttributes:greenAttrs range:NSMakeRange(0,2)];
NSMutableAttributedString *orangeString = [[NSMutableAttributedString alloc] initWithString:stringTres];
[orangeString setAttributes:orangeAttrs range:NSMakeRange(0,4)];
NSArray *myStrings = [[NSArray alloc] initWithObjects:redString, greenString, orangeString, nil];
NSLog(#"%#", [myStrings description]);
NSMutableAttributedString *result = [[NSMutableAttributedString alloc]init];
NSAttributedString *delimiter = [[NSAttributedString alloc] initWithString: #", "];
for (NSAttributedString *str in myStrings) {
if (result.length) {
[result appendAttributedString:delimiter];
}
[result appendAttributedString:str];
}
_lblUm.attributedText = result;
Your question is very unclear. But based on your comment to gerrytan's answer, your goal is clearer.
If you have an array of NSAttributedString objects, then you can create a single string by appending them all together with an NSMutableAttributedString.
NSArray *myStrings = ... // your array of NSAttributedString objects
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
// Put this delimiter between each string - change as desired
NSAttributedString *delimiter = [[NSAttributedString alloc] initWithString:#", "];
for (NSAttributeString *str in myStrings) {
if (result.length) {
[result appendAttributedString:delimiter];
}
[result appendAttributedString:str];
}
myLabel.attributedText = result;
UILabel only supports one NSAttributedString. I think what you can do is to place multiple UILabel side by side for each string on the array

Append a NSString as the first line of another NSString

I have two NSString, A and B.
I would that A becomes B\nA.
How can I do?
If in a method I use
NSString *string_B = [[NSString alloc] initWithString:#"something_from_a_DB"];
NSString *string_A = [[NSString alloc] initWithString:#"something_from_a_DB"];
if (aTrueCondition) {
string_C = [NSString stringWithFormat:#"%#\n%#", string_B, string_A];
} else {
string_C = string_A;
}
is string_C = string_A; a memory leak or is it good?
I added [string_A release], as string_C is a retained property. Now it works.
This is the way to put them together:
NSString *newString = [NSString stringWithFormat:#"%#\n%#", stringB, stringA];
The second part is “A becoming newString”. This is hard to do, as regular strings are immutable in Cocoa. The best thing you can do is throw out the old A and point A to the new string:
NSString *strA = #"foo";
NSString *strB = #"bar";
strA = [NSString stringWith…];
Just be careful not to leak A:
NSString *strA = [[NSString alloc] initWithString:#"foo"];
strA = [NSString stringWith…]; // this is a leak
NSString *str=[NSString stringWithFormat:#"%#\n%#",B,A];
use this.
NSString *stringA = [NSString stringWithFormat:#"%#\n%#", stringB, stringA];