Search code examples
iphoneobjective-cios6xcode4nsattributedstring

attributed string which can accept instances depending upon what is passed from a table view


I have a tableView which users will select an instance. I have segue set-up to pass the instance to the textView. I have an NSLog setup so I know the correct data is being passed. I want to setup an NSAttributedString to accept the data and import the relevant portions into the attributedString template.

Seems to me it should look like this:

displayText = [[NSMutableAttributedString alloc] initWithString:@"%@\n%@-%@",detailName, beginDate, endDate") attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:26]}];

My ideal formatting is would be to create the string and have identify the name, begin, and end spaces in a way that allows them to have individual attributes. I've tried every combination of moving pieces but I simply cannot make this work. I receive errors such as Incompatible pointer types sending NSString to NSAttributedString, too many arguments, etc.

I'm new to programming and learning as I go but I've bought books and watched videos but I cannot figure out where I'm going wrong. Thanks for the help.

Isaac


Solution

  • That's because you need to use NSString's stringWithFormat: in order to pass variables to the string. Here's an example: (and there was an extraneous quotation mark after the argument list)

    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:26]};
    
    NSMutableAttributedString *displayText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@-%@",detailName, beginDate, endDate] attributes:attributes];