Wednesday 18 March 2015

NSAttributedString with fonts, paragraph Style, line spacing using NSMutableAttributedString

// create a Attributed string and adding different parameters like font, paragraph Style, etc

    // Create UIFont -
    UIFont *systemFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0];

    // Create a paragraphStyle -
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.maximumLineHeight = 45;
    paragraphStyle.minimumLineHeight = 30;
    paragraphStyle.lineSpacing = 15;
    paragraphStyle.lineHeightMultiple = 10;
    
    // create a dictionary of Attributed -
    NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont,  NSFontAttributeName,NSParagraphStyleAttributeName,paragraphStyle, nil];
    
    // create a Attributed  string and adding parameters dictionary-
    NSMutableAttributedString *libTitle = [[NSMutableAttributedString alloc] initWithString:@"Hello there, \n" attributes:fontAttributes];
    

    // create second Attributed  string with different font-
    UIFont *subTextFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:15.0];

    NSDictionary * subTitlefontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:subTextFont, NSFontAttributeName,, nil];

    NSMutableAttributedString *subTitleString = [[NSMutableAttributedString alloc] initWithString:@"MacOS & iOS developments tips" attributes: subTitlefontAttributes];
    

    // Append Attributed string and create a single string-
    [libTitle appendAttributedString: subTitleString];

    // Set Attributed
 string on the label
    [self.myLabel setAttributedText:libTitle];