以前文本整理有一點亂,這邊從新整理一下,下面是效果圖,一共兩個UILabel,瀏覽器
富文本整理:app
/*NSForegroundColorAttributeName設置字體顏色,對象UIColor; NSParagraphStyleAttributeName設置段落格式,對象NSMutableParagraphStyle; NSFontAttributeName設置字體,對象UIFont; NSBackgroundColorAttributeName設置背景顏色,對象UIColor; NSKernAttributeName設置字符間距,對象NSNumber NSStrikethroughStyleAttributeName是指刪除線樣式 對象NSUnderlineStyle NSStrikethroughColorAttributeName刪除線顏色,對象UIColor NSUnderlineStyleAttributeName設置下劃線,參考刪除線 NSUnderlineColorAttributeName設置下劃線顏色 NSStrokeWidthAttributeName設置筆畫寬度,對象NSNumber,負值填充效果,正值中空效果 NSStrokeColorAttributeName當文字中空或者填充設置文字的描邊顏色,對象UIColor; NSShadowAttributeName 設置陰影,NSShadow對象 NSBaselineOffsetAttributeName設置基線偏移值,對象NSNumber (float),正值上偏,負值下偏 NSObliquenessAttributeName設置字形傾斜度,對象NSNumber (float),正值右傾,負值左傾 NSExpansionAttributeName 設置文本橫向拉伸屬性,對象NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本 NSVerticalGlyphFormAttributeName 設置文字排版方向,取值爲 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本 NSLinkAttributeName設置連接屬性,點擊後調用瀏覽器打開指定URL地址,對象NSString,NSUrl,這個只能UITextView可用UITextView的代理方法中shouldInteractWithURL返回true可用 NSTextEffectAttributeName設置文本特殊效果,目前只有一個可用效果 NSTextEffectLetterpressStyle(凸版印刷效果 設置連體屬性,取值爲NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符//沒有測出效果 */
源碼展現:ide
NSString *text = @"池塘大橋下,遊過一羣鴨,快來快來數一數,二四六七八,嘎嘎嘎嘎,真呀真多呀,fly,數不清到底多少鴨"; NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; //行間距爲 [paragraphStyle setLineSpacing:8]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : [UIColor blueColor], NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}] ; //設置特殊字體文本格式 NSRange range = [text rangeOfString:@"池塘大橋下"]; [string addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSBackgroundColorAttributeName:[UIColor yellowColor],NSKernAttributeName:@(12),NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName:[UIColor purpleColor]} range:range];//添加屬性 NSRange range1 = [text rangeOfString:@"遊過"]; [string addAttributes:@{NSStrokeWidthAttributeName:@(-2),NSStrokeColorAttributeName:[UIColor redColor]} range:range1];//添加屬性 NSRange range2 = [text rangeOfString:@"一羣鴨"]; NSShadow *shadow = [NSShadow new]; [shadow setShadowColor:[UIColor redColor]]; [shadow setShadowOffset:CGSizeMake(5.0f, 5.0f)];//設置x,y軸偏移量 [shadow setShadowBlurRadius:1.0];//設置模糊度 [string addAttributes:@{NSShadowAttributeName:shadow} range:range2];//添加屬性 NSRange range3 = [text rangeOfString:@"快來快來"]; [string addAttributes:@{NSBaselineOffsetAttributeName:@(10)} range:range3];//添加屬性 NSRange range4 = [text rangeOfString:@"數一數"]; [string addAttributes:@{NSObliquenessAttributeName:@(0.5)} range:range4];//添加屬性 NSRange range5 = [text rangeOfString:@"二四"]; [string addAttributes:@{NSExpansionAttributeName:@(0.8)} range:range5];//添加屬性 NSRange range6 = [text rangeOfString:@"六七八"]; [string addAttributes:@{NSExpansionAttributeName:@(-0.8)} range:range6];//添加屬性 /** 沒有測出具體的區別 NSWritingDirectionAttributeName 設置文字書寫方向,從左向右書寫或者從右向左書寫 @[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)] @[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)] @[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)] @[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)] */ [string addAttributes:@{NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]} range:NSMakeRange(0, text.length)];//添加屬性 [string addAttributes:@{NSVerticalGlyphFormAttributeName:@1} range:NSMakeRange(0, text.length)];//添加屬性 NSRange range8 = [text rangeOfString:@"嘎嘎嘎嘎"]; NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; [string addAttributes:@{NSLinkAttributeName:url} range:range8];//添加屬性 NSRange range9 = [text rangeOfString:@"真呀真多呀"]; [string addAttributes:@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle} range:range9];//添加屬性 NSRange range10 = [text rangeOfString:@"fly"]; [string addAttributes:@{NSLigatureAttributeName:@0} range:range10];//添加屬性 self.label.attributedText = string;
NSMutableParagraphStyle用法整理:字體
NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; //使用一張圖片做爲Attachment數據 attachment.image = [UIImage imageNamed:@"chepaihao"]; //這裏bounds的x值並不會產生影響 attachment.bounds = CGRectMake(5, (self.label1.font.lineHeight - self.label1.font.pointSize) / 2, 20, 10); NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle1 setLineSpacing:20]; //首行縮進 [paragraphStyle1 setFirstLineHeadIndent:20]; [paragraphStyle1 setLineBreakMode:NSLineBreakByTruncatingMiddle]; //兩端對齊的)文本對齊方式 paragraphStyle1.minimumLineHeight = 10;//最低行高 paragraphStyle1.maximumLineHeight = 20;//最大行高 paragraphStyle1.paragraphSpacing = 45;//段與段之間的間距 // paragraphStyle1.paragraphSpacingBefore = 1000;//段首行空白空間 paragraphStyle1.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共➡️三種) // paragraphStyle1.lineHeightMultiple = -100;沒有測出效果 // paragraphStyle1.hyphenationFactor = 1;//連字屬性 在iOS,惟一支持的值分別爲0和1 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人" attributes:@{NSParagraphStyleAttributeName: paragraphStyle1}]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; [attributedString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"\n你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人,你是一個好人" attributes:@{NSParagraphStyleAttributeName: paragraphStyle1}]]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; self.label1.backgroundColor = [UIColor redColor]; // self.label1.textAlignment = NSTextAlignmentCenter; self.label1.attributedText = attributedString; // self.label1.lineBreakMode = NSLineBreakByCharWrapping;
獲取富文本的高度和普通label的高度同樣:url
CGSize attSize = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;