改變textview或lab等多行字體的大小與顏色等變化

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.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容