NSAttributedString
发布日期:2022-03-18 08:27:41 浏览次数:18 分类:技术文章

本文共 24684 字,大约阅读时间需要 82 分钟。

字符属性

在中列举的Standard Attributes如下:

这里写图片描述
在中,列举的字符属性有:

/* 字符属性 字符属性可以应用于 attributed string 的文本中。 NSString *const NSFontAttributeName;(字体) NSString *const NSParagraphStyleAttributeName;(段落) NSString *const NSForegroundColorAttributeName;(字体颜色) NSString *const NSBackgroundColorAttributeName;(字体背景色) NSString *const NSLigatureAttributeName;(连字符) NSString *const NSKernAttributeName;(字间距) NSString *const NSStrikethroughStyleAttributeName;(删除线) NSString *const NSUnderlineStyleAttributeName;(下划线) NSString *const NSStrokeColorAttributeName;(边线颜色) NSString *const NSStrokeWidthAttributeName;(边线宽度) NSString *const NSShadowAttributeName;(阴影)(横竖排版) NSString *const NSVerticalGlyphFormAttributeName; 常量 1> NSFontAttributeName(字体) 该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。 2> NSParagraphStyleAttributeName(段落) 该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。 3> NSForegroundColorAttributeName(字体颜色) 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。 4> NSBackgroundColorAttributeName(字体背景色) 该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。 5> NSLigatureAttributeName(连字符) 该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。 6> NSKernAttributeName(字间距) 该属性所对应的值是一个 NSNumber 对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为 0 表示不使用字母紧排。默认值为0。 7> NSStrikethroughStyleAttributeName(删除线) 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。 8> NSUnderlineStyleAttributeName(下划线) 该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。 9> NSStrokeColorAttributeName(边线颜色) 该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。 10> NSStrokeWidthAttributeName(边线宽度) 该属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。 11> NSShadowAttributeName(阴影) 该属性所对应的值是一个 NSShadow 对象。默认为 nil。 12> NSVerticalGlyphFormAttributeName(横竖排版) 该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。 */

使用方法有:

这里写图片描述


下面用一些例子来说明

NSBackgroundColorAttributeName字体背景色

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:infoString];    [attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:17.0f] range:NSMakeRange(0, infoString.length)];    [attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, infoString.length)];    [attString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];

效果如下:

这里写图片描述

NSKernAttributeName字间距

在上面的例子的基础上改变字间距

[attString addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:3] range:NSMakeRange(0, infoString.length)];

这里写图片描述

NSStrikethroughStyleAttributeName删除线&NSUnderlineStyleAttributeName下划线

underline线枚举为NSUnderlineStyle,有多个值:

// NSUnderlineStyleNone   不设置删除线// NSUnderlineStyleSingle 设置删除线为细单实线// NSUnderlineStyleThick  设置删除线为粗单实线// NSUnderlineStyleDouble 设置删除线为细双实线//删除线[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, 15)];[attString addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, infoString.length)];//下划线[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSUnderlineColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, infoString.length)];

这里写图片描述

NSStrokeColorAttributeName边线颜色&NSStrokeWidthAttributeName边线宽度

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:infoString];[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:25.0f] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:-5] range:NSMakeRange(0, infoString.length)];

这里写图片描述

NSShadowAttributeName阴影

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:infoString];[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:25.0f] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, infoString.length)];NSShadow *shadowDic=[[NSShadow alloc] init];[shadowDic setShadowBlurRadius:5.0];[shadowDic setShadowColor:[UIColor grayColor]];[shadowDic setShadowOffset:CGSizeMake(0, 3)];[attString addAttribute:NSShadowAttributeName value:shadowDic range:NSMakeRange(0, infoString.length)];

这里写图片描述

NSLinkAttributeName链接

链接属性点击将启动浏览器打开一个URL地址,中间用到一个代理函数,UILabel 和 UITextField 无法使用该属性,所以只能用UITextView来做示例。

_textView.editable = NO;        //必须禁止输入,否则点击将弹出输入键盘_textView.scrollEnabled = NO;   //可选_textView.delegate = self;      //必须设置,否则代理函数不会被回调NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"谷歌百度www.google.com"];NSDictionary *linkDic = @{ NSLinkAttributeName : [NSURL URLWithString:@"http://www.google.com"] };[str setAttributes:linkDic range:[[str string] rangeOfString:@"www.google.com"]];_textView.attributedText = str;

这里写图片描述

NSParagraphStyleAttributeName段落

NSString *infoString = @"This is an example of Attributed String";NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:infoString];[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:25.0f] range:NSMakeRange(0, infoString.length)];[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, infoString.length)];NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];paragraph.alignment = NSTextAlignmentCenter;paragraph.hyphenationFactor = .9;//连字符[attString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, infoString.length)];

这里写图片描述

NSAttachmentAttributeName

NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];textAttachment.image = [UIImage imageNamed: @"e01"];  //设置图片源textAttachment.bounds = CGRectMake(0, 0, 30, 30);NSString *string = @"This is example of attributes string";NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];[attString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, string.length)];NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:textAttachment];[attString insertAttributedString:attachString atIndex:4];_textView.attributedText = attString;

这里写图片描述

其他

请参考

  • NSTextEffectAttributeName:设置文本特殊效果,取值为 NSString 对象,目前只有一个可用的特效: NSTextEffectLetterpressStyle(凸版印刷效果),适用于iOS 7.0及以上

    这里写图片描述

  • NSBaselineOffsetAttributeName:设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏

    这里写图片描述

  • NSObliquenessAttributeName:设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾

    这里写图片描述

  • NSExpansionAttributeName:设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本

    这里写图片描述

  • NSWritingDirectionAttributeName:设置文字书写方向,取值为以下组合

    //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]//@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]//@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]//@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]

    这里写图片描述


NSParagraphStyleAttributeName:设置文本段落排版格式,取值为 NSParagraphStyle/NSMutableParagraphStyle 对象,可以设置如下属性:

// alignment               对齐方式,取值枚举常量 NSTextAlignment    // firstLineHeadIndent     首行缩进,取值 float    // headIndent              缩进,取值 float    // tailIndent              尾部缩进,取值 float    // ineHeightMultiple       可变行高,乘因数,取值 float    // maximumLineHeight       最大行高,取值 float    // minimumLineHeight       最小行高,取值 float    // lineSpacing             行距,取值 float    // paragraphSpacing        段距,取值 float    // paragraphSpacingBefore  段首空间,取值 float    //        // baseWritingDirection    句子方向,取值枚举常量 NSWritingDirection    // lineBreakMode           断行方式,取值枚举常量 NSLineBreakMode    // hyphenationFactor       连字符属性,取值 0 - 1
  • alignment

    alignment 对齐方式,取值枚举常量 NSTextAlignment

    //    enum {//        NSTextAlignmentLeft      = 0,//        NSTextAlignmentCenter    = 1,//        NSTextAlignmentRight     = 2,//        NSTextAlignmentJustified = 3,//        NSTextAlignmentNatural   = 4,//    };//    typedef NSInteger NSTextAlignment;_textview01.editable = NO;_textview02.editable = NO;_label11.text = @"alignment : NSTextAlignmentCenter";_label12.text = @"alignment : NSTextAlignmentJustified";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;   // NSParagraphStyle    NSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];    paraStyle01.alignment = NSTextAlignmentNatural;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.alignment = NSTextAlignmentJustified;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述这里写图片描述

  • firstLineHeadIndent

    firstLineHeadIndent 首行缩进,取值 float

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"firstLineHeadIndent: 24";_label12.text = @"firstLineHeadIndent: 48";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.firstLineHeadIndent = 24;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.firstLineHeadIndent = 48;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • headIndent

    headIndent 除了首行之外的行缩进,取值 float

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"headIndent: 24";_label12.text = @"headIndent: 48";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.headIndent = 24;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.headIndent = 48;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • tailIndent

    tailIndent 行尾缩进,注意距离是从行首算起

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"tailIndent: 48";_label12.text = @"tailIndent: 252";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.tailIndent = 48;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.tailIndent = 252;NSDiction![这里写图片描述](https://img-blog.csdn.net/20151009151758641)ary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • lineHeightMultiple

    lineHeightMultiple 行高倍数因子,大于1行高变大,小于1行高变小,实际上字体大小不会改变,改变的时行间距

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"lineHeightMultiple: 0.6";_label12.text = @"lineHeightMultiple: 2.5";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.lineHeightMultiple = 0.6;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.lineHeightMultiple = 2.5;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • maximumLineHeight

    maximumLineHeight 最大行高,若其值小于默认行高,则行间距变小,若其值大于默认行高,则不会引起任何变化

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"maximumLineHeight: 7";_label12.text = @"maximumLineHeight: 25";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.maximumLineHeight = 7;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.maximumLineHeight = 25;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • minimumLineHeight

    minimumLineHeight 最小行高,若其值大于默认行高,则行间距变大,若其值小于默认行高,则不会引起任何变化

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"minimumLineHeight: 0.6";_label12.text = @"minimumLineHeight: 25";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.minimumLineHeight = 0.6;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.minimumLineHeight = 25;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • lineSpacing

    lineSpacing 行距,取值为 float,可正可负,正值增加行距,负值减小行距

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"lineSpacing: -7";_label12.text = @"lineSpacing: 25";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.lineSpacing = -7;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.lineSpacing = 25;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • paragraphSpacing

    paragraphSpacing 段距,取值 float, 负值无效,取0值

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"paragraphSpacing: -7";_label12.text = @"paragraphSpacing: 25";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.paragraphSpacing = -7;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.paragraphSpacing = 25;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • paragraphSpacingBefore

    paragraphSpacingBefore 段首距离,取值 float , 最小取值为0

    _textview01.editable = NO;_textview02.editable = NO;_label11.text = @"paragraphSpacingBefore: -7";_label12.text = @"paragraphSpacingBefore: 25";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.paragraphSpacingBefore = -7;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.paragraphSpacingBefore = 25;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • baseWritingDirection

    baseWritingDirection句子排版方向,取值为枚举常量 NSWritingDirection

    //    enum {//        NSWritingDirectionNatural = -1,//        NSWritingDirectionLeftToRight =  0,//        NSWritingDirectionRightToLeft =  1//    };//    typedef NSInteger NSWritingDirection;_textview01.editable = NO;_textview02.editable = NO;_label11.text = @"baseWritingDirection: NSWritingDirectionLeftToRight";_label12.text = @"baseWritingDirection: NSWritingDirectionRightToLeft";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.baseWritingDirection = NSWritingDirectionLeftToRight;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.baseWritingDirection = NSWritingDirectionRightToLeft;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述

  • lineBreakMode

    lineBreakMode 断行方式,取值枚举常量 NSLineBreakMode

    //    enum {//        NSLineBreakByWordWrapping = 0, //自动换行,单词切断//        NSLineBreakByCharWrapping,     //自动换行,字母切断//        NSLineBreakByClipping,         //非自动换行,不切断//        NSLineBreakByTruncatingHead,   //非自动换行,行首切断//        NSLineBreakByTruncatingTail,   //非自动换行,行尾切断//        NSLineBreakByTruncatingMiddle  //非自动换行,中间切断//    };//    typedef NSUInteger NSLineBreakMode;_textview01.editable = NO;_textview02.editable = NO;_label11.text = @"lineBreakMode: NSLineBreakByTruncatingHead";_label12.text = @"lineBreakMode: NSLineBreakByTruncatingTail";NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";NSString *text = [NSString stringWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"荷塘月色" ofType:@"txt"] encoding: NSUTF8StringEncoding error: NULL] ;// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.lineBreakMode = NSLineBreakByTruncatingHead;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.lineBreakMode = NSLineBreakByTruncatingTail;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 12] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: text attributes: attrDict02];

    这里写图片描述这里写图片描述这里写图片描述

  • hyphenationFactor

    hyphenationFactor 连字符属性,取值 0 到 1 之间,开启断词功能

    NSString *strstr = @"These two documents provide the perfect starting point for iOS and Mac app development. Follow either road map to learn how to get and use Xcode to create your first app. You will learn how to use Xcode to test and debug your source code, analyze to improve your app’s performance, perform source control operations, archive your app, and submit your app to the App Store.";_textview01.editable = NO;_textview02.editable = NO;_label11.text = @"hyphenationFactor: 0.3";_label12.text = @"hyphenationFactor: 0.9";// NSParagraphStyleNSMutableParagraphStyle *paraStyle01 = [[NSMutableParagraphStyle alloc] init];paraStyle01.hyphenationFactor = 0.3;NSDictionary *attrDict01 = @{ NSParagraphStyleAttributeName: paraStyle01,                              NSFontAttributeName: [UIFont systemFontOfSize: 15] };_textview01.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict01];NSMutableParagraphStyle *paraStyle02 = [[NSMutableParagraphStyle alloc] init];paraStyle02.hyphenationFactor = 0.9;NSDictionary *attrDict02 = @{ NSParagraphStyleAttributeName: paraStyle02,                              NSFontAttributeName: [UIFont systemFontOfSize: 15] };_textview02.attributedText = [[NSAttributedString alloc] initWithString: strstr  attributes: attrDict02];

    这里写图片描述

转载地址:https://windzen.blog.csdn.net/article/details/53609894 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UILabel&UITextView文本嵌入图片处理
下一篇:如何处理文字中的emoji?

发表评论

最新留言

不错!
[***.144.177.141]2023年05月07日 16时46分44秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

最新文章