改变textview或lab等多行字体的大小与颜色等变化

2017-11-23  本文已影响17人  水月dragon

self.textView= [[UITextViewalloc]initWithFrame:CGRectMake(15,100,self.view.frame.size.width-40,40)];

self.textView.showsVerticalScrollIndicator=NO;

self.textView.contentInset=UIEdgeInsetsMake(-5,0,0,0);

self.textView.delegate=self;

[self.viewaddSubview:self.textView];

self.textView.attributedText=  [selftwoFontWithStr1:@"ceshisa"TwoFontWithStr2:@"fdfvfghjghjfghfghjghghghjkldsa"font1:[UIFontsystemFontOfSize:18.f]font2:[UIFontsystemFontOfSize:12.f]color1:[UIColorredColor]color2:[UIColorblueColor]];

- (NSMutableAttributedString*)twoFontWithStr1:(NSString* )str1 TwoFontWithStr2:(NSString*)str2 font1:(UIFont*)font1 font2:(UIFont*)font2 color1:(UIColor*)color1 color2:(UIColor*)color2{

NSIntegerlen1 = str1.length;

NSIntegerlen2 = str2.length;

NSString* allStr = [NSStringstringWithFormat:@"%@\n%@",str1,str2];

NSIntegerlenAll = allStr.length;

NSLog(@"总字符%@,长度1%ld,长度2%ld,长度总%ld",allStr,len1,len2,lenAll);

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStylealloc]init];

paragraphStyle.lineSpacing= -20;

paragraphStyle.alignment=NSTextAlignmentCenter;

NSDictionary* dic =@{NSParagraphStyleAttributeName:paragraphStyle};

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc]initWithString:allStrattributes:dic];

[straddAttribute:NSFontAttributeNamevalue:font1range:NSMakeRange(0, len1)];

[straddAttribute:NSForegroundColorAttributeNamevalue:color1range:NSMakeRange(0, len1)];

[straddAttribute:NSFontAttributeNamevalue:font2range:NSMakeRange(len1 +1, len2)];

[straddAttribute:NSForegroundColorAttributeNamevalue:color2range:NSMakeRange(len1 +1, len2)];

returnstr;

}

// 通过改变属性值来改变对应的字体大小颜色等,同事也可以增加字符串或者获取一段字符串中的一段字符进行修改

// Predefined character attributes for text. If the key is not present in the dictionary, it indicates the default value described below.

UIKIT_EXTERNNSAttributedStringKeyconstNSFontAttributeNameNS_AVAILABLE(10_0,6_0);// UIFont, default Helvetica(Neue) 12

UIKIT_EXTERNNSAttributedStringKeyconstNSParagraphStyleAttributeNameNS_AVAILABLE(10_0,6_0);// NSParagraphStyle, default defaultParagraphStyle

UIKIT_EXTERNNSAttributedStringKeyconstNSForegroundColorAttributeNameNS_AVAILABLE(10_0,6_0);// UIColor, default blackColor

UIKIT_EXTERNNSAttributedStringKeyconstNSBackgroundColorAttributeNameNS_AVAILABLE(10_0,6_0);// UIColor, default nil: no background

UIKIT_EXTERNNSAttributedStringKeyconstNSLigatureAttributeNameNS_AVAILABLE(10_0,6_0);// NSNumber containing integer, default 1: default ligatures, 0: no ligatures

UIKIT_EXTERNNSAttributedStringKeyconstNSKernAttributeNameNS_AVAILABLE(10_0,6_0);// NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.

UIKIT_EXTERNNSAttributedStringKeyconstNSStrikethroughStyleAttributeNameNS_AVAILABLE(10_0,6_0);// NSNumber containing integer, default 0: no strikethrough

UIKIT_EXTERNNSAttributedStringKeyconstNSUnderlineStyleAttributeNameNS_AVAILABLE(10_0,6_0);// NSNumber containing integer, default 0: no underline

UIKIT_EXTERNNSAttributedStringKeyconstNSStrokeColorAttributeNameNS_AVAILABLE(10_0,6_0);// UIColor, default nil: same as foreground color

UIKIT_EXTERNNSAttributedStringKeyconstNSStrokeWidthAttributeNameNS_AVAILABLE(10_0,6_0);// NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

UIKIT_EXTERNNSAttributedStringKeyconstNSShadowAttributeNameNS_AVAILABLE(10_0,6_0);// NSShadow, default nil: no shadow

UIKIT_EXTERNNSAttributedStringKeyconstNSTextEffectAttributeNameNS_AVAILABLE(10_10,7_0);// NSString, default nil: no text effect

UIKIT_EXTERNNSAttributedStringKeyconstNSAttachmentAttributeNameNS_AVAILABLE(10_0,7_0);// NSTextAttachment, default nil

UIKIT_EXTERNNSAttributedStringKeyconstNSLinkAttributeNameNS_AVAILABLE(10_0,7_0);// NSURL (preferred) or NSString

UIKIT_EXTERNNSAttributedStringKeyconstNSBaselineOffsetAttributeNameNS_AVAILABLE(10_0,7_0);// NSNumber containing floating point value, in points; offset from baseline, default 0

UIKIT_EXTERNNSAttributedStringKeyconstNSUnderlineColorAttributeNameNS_AVAILABLE(10_0,7_0);// UIColor, default nil: same as foreground color

UIKIT_EXTERNNSAttributedStringKeyconstNSStrikethroughColorAttributeNameNS_AVAILABLE(10_0,7_0);// UIColor, default nil: same as foreground color

UIKIT_EXTERNNSAttributedStringKeyconstNSObliquenessAttributeNameNS_AVAILABLE(10_0,7_0);// NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

UIKIT_EXTERNNSAttributedStringKeyconstNSExpansionAttributeNameNS_AVAILABLE(10_0,7_0);// NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

UIKIT_EXTERNNSAttributedStringKeyconstNSWritingDirectionAttributeNameNS_AVAILABLE(10_6,7_0);// NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values.  LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,

UIKIT_EXTERNNSAttributedStringKeyconstNSVerticalGlyphFormAttributeNameNS_AVAILABLE(10_7,6_0);// An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.

上一篇下一篇

猜你喜欢

热点阅读