NSTextView making bold text using an array

心已入冬 提交于 2019-12-11 09:42:45

问题


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'


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/23840682/nstextview-making-bold-text-using-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!