简体   繁体   中英

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 attributedString: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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM