UILable文本屬性設置

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSAttributeString 可以用來設置字體、段落樣式,字體顏色,字體背景顏色,可以添加刪除線、下劃線,可以設置字間距、陰影、空心字、斜體、扁平化"];

    [attributedString addAttribute:NSExpansionAttributeName value:@1 range:NSMakeRange(0, 17)];  // 扁平化

    [attributedString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(18, 8)];// 傾斜

    

    

    //段落

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.firstLineHeadIndent = 80;// 首行縮進

    paragraphStyle.headIndent = 25; // 其它行縮進

    paragraphStyle.lineSpacing = 20;   // 行間距

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];// 段落

    

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowBlurRadius = 5; //模糊度

    shadow.shadowColor = [UIColor redColor];

    shadow.shadowOffset = CGSizeMake(1, 3);

    [attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:NSMakeRange(27, 4)];

    [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(27, 4)];

    [attributedString addAttribute:NSStrokeWidthAttributeName value:@(-3.0) range:NSMakeRange(32, 11)];// 邊線寬度

    [attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(32, 11)];//邊線顏色,需要先設置邊線寬度

    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(44, 7)]; // 刪除線

    [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(52, 3)]; // 下劃線

    

    [attributedString setAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldItalicMT" size:18],  NSKernAttributeName:@(10),NSForegroundColorAttributeName:[UIColor redColor], NSBackgroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(56, 20)];

    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor grayColor];

    label.attributedText = attributedString;

    [self.view addSubview:label];


#pragma mark

頂上

- (void)alignTop;


/**

 *  改變行間距

 */

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;


/**

 *  改變字間距

 */

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;


/**

 *  改變行間距和字間距

 */

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;


- (void)alignTop {

    CGSize fontSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2]];

    

    double finalHeight = fontSize.height * self.numberOfLines;

    double finalWidth = self.frame.size.width;    //expected width of label

    CGSize theStringSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2] constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];

    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;

    for(int i=0; i<newLinesToPad; i++)

        self.text = [self.text stringByAppendingString:@"\n "];

}


+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:space];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:lineSpace];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章