ios uilabel shows pictures and various attributes that uilabel is very powerful!!!

Keywords: Attribute


The code is as follows

Method calls are as follows

NSString  * Str = @"Long live the Chinese People's Liberation Army, the People's Republic of China, long live!";
    self.attrobiuteLabel.attributedText = [self stringWithUIImage:Str];
// Add a single attribute
//- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

// Create a Rich Text
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];


// Modifying the Styles of Different Texts in Rich Texts
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];

    /**
     Add the image to the specified location
     */
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    // Expression pictures
    attchImage.image = [UIImage imageNamed:@"jiedu"];
    // Set Picture Size
    attchImage.bounds = CGRectMake(0, 0, 40, 15);
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    [attriStr insertAttributedString:stringImage atIndex:2];

 // Set the number to red
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];



    // Add expression to last place
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // Expression pictures
    attch.image = [UIImage imageNamed:@"jiedu"];
    // Set Picture Size
    attch.bounds = CGRectMake(0, 0, 40, 15);

    // Create Rich Text with Pictures
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    [attriStr appendAttributedString:string];
// Adding group attributes
// - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;

The fastest way to do this is to set attributes
NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
    [attriStr addAttributes:attriBute range:NSMakeRange(5, 9)];



The whole method is as follows

- (NSAttributedString *)stringWithUIImage:(NSString *) contentStr {

    // Create a Rich Text
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    // Modifying the Styles of Different Texts in Rich Texts
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];


    /**
     Add the image to the specified location
     */
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    // Expression pictures
    attchImage.image = [UIImage imageNamed:@"jiedu"];
    // Set Picture Size
    attchImage.bounds = CGRectMake(0, 0, 40, 15);
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    [attriStr insertAttributedString:stringImage atIndex:2];


    // Set the number to red
    /*
    [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)];
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];
    */
    //NSDictionary * attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
                               // NSForegroundColorAttributeName: [UIColor blueColor] };

    //Create NSAttributedString and assign values
    //_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];
    NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
    [attriStr addAttributes:attriBute range:NSMakeRange(5, 9)];

    // Add expression to last place
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // Expression pictures
    attch.image = [UIImage imageNamed:@"jiedu"];
    // Set Picture Size
    attch.bounds = CGRectMake(0, 0, 40, 15);

    // Create Rich Text with Pictures
    NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
    [attriStr appendAttributedString:string];

    return attriStr;
}



Finally, get to know the meaning of each attribute.

// NSFontAttributeName sets font properties, default value: font: Helvetica(Neue) font size: 12
 // NSForegroundColorAttributeNam sets the font color to UIColor object and defaults to black.
// NSBackgroundColorAttributeName sets the background color of the area where the font is located, and takes the value as UIColor object. The default value is nil and transparent.
// NSLigatureAttributeName sets the concatenated property to an NSNumber object (integer), 0 to indicate that there is no concatenated character, and 1 to indicate that the default concatenated character is used.
// NSKernAttributeName sets the character spacing as an NSNumber object (integer), widens the positive value spacing and narrows the negative value spacing.
// NSStrikethrough StyleAttributeName sets the delete line to the NSNumber object (integer)
// NSStrikethroughColorAttributeName sets the delete line color to UIColor object and defaults to black
 // NSUnderline StyleAttributeName sets the underscore to the NSNumber object (integer) and enumerates the values in the constant NSUnderline Style, similar to the deletion line.
// NSUnderline ColorAttribute Name sets the underline color to UIColor object and defaults to black
 // NSStrokeWidthAttributeName sets the stroke width to NSNumber object (integer), negative filling effect, and positive hollow effect.
// NSStrokeColorAttributeName fills in part of the color, not the font color, with the value UIColor object
 // NSShadowAttributeName sets the shadow attribute to the NSShadow object
 // NSTextEffectAttributeName sets the special effect of text as an NSString object. At present, only the effect of plate printing is available:
// NSBaseline Offset Attribute Name sets the baseline offset value as NSNumber (float), positive offset and negative offset.
// NSObliqueness Attribute Name sets the glyph tilt to NSNumber (float), positive to right and negative to left.
// NSExpansion Attribute Name sets the text horizontal stretching property to NSNumber (float), positive horizontal stretching text, negative horizontal compression text.
// NSWriting Direction Attribute Name sets the direction of writing, writing from left to right or from right to left
 // NSVertical GlyphFormAttributeName sets the direction of text typesetting, takes the value NSNumber object (integer), 0 represents horizontal text, 1 represents vertical text.
// NSLinkAttributeName sets the link properties and calls the browser to open the specified URL address after clicking
 // NSAttachment Attribute Name sets text attachments to NSTextAttachment objects, often used for text and image mixing
 // NSParagraphStyleAttributeName sets the format of text paragraph typesetting with the value of NSParagraphStyle object

Posted by sdlyr8 on Fri, 29 Mar 2019 02:42:29 -0700