您的位置:首页 > 产品设计 > UI/UE

UILabletext去掉乱码 控制颜色 行高 自定义大小 。显示不同的字体颜色、字体大小、行间距、首行缩进、下划线等属性(NSMutableAttributedString)

2017-03-25 14:29 726 查看
text去掉乱码 设置不同颜色 行高 自定义大小

#import <Foundation/Foundation.h>

@interface TextsForRow : NSObject

@property(nonatomic,copy)NSString * string;

/**
文本包含了 标题+文本。 使用前设置内容的颜色
操作中:标题设置颜色。文本颜色   标题+文本字体大小 行间距 以及返回高度

@param stringTitle title文本
@param colorTitle title颜色
@param stringText 内容text
@return 返回数组3个。 1 返回的 NSMutableAttributedString * strAttebute;;2返回的 宽度0.2f的string,使用时转化 3:高度
*/
+(NSArray *)TextsForRowWithStringTitle:(NSString*)stringTitle ColorWith:(UIColor*)colorTitle textWithStringText:(NSString*)stringText;
@end


#import "TextsForRow.h"

@implementation TextsForRow

+(NSArray *)TextsForRowWithStringTitle:(NSString*)stringTitle ColorWith:(UIColor*)colorTitle textWithStringText:(NSString*)stringText{

//title+text
NSString * str1 =[NSString stringWithFormat:@"%@%@",stringTitle,stringText];

//删除不需要的个别字符
NSString * str = [str1  stringByReplacingOccurrencesOfString:@"<DIV>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"</DIV>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"<BR>" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"</BR>" withString:@""];

//删除讨厌的字符
NSRegularExpression * regu2 =[NSRegularExpression regularExpressionWithPattern:@"(?:<|</|>| )" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *string3 = [regu2 stringByReplacingMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length) withTemplate:@""];

//去掉左右两边的空格
NSString * kongge = [string3 stringByReplacingOccurrencesOfString:@" " withString:@""];

//去掉左右两边的空格
NSString * string = [kongge stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSMutableAttributedString * strAttebute = [[NSMutableAttributedString alloc] initWithString:string ];
//设置title颜色
[strAttebute addAttribute:NSForegroundColorAttributeName value:colorTitle range:NSMakeRange(0, stringTitle.length)]
;
//行间距
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:IPHONEHIGHT(10)];
[strAttebute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

//自定义大小
CGSize contentSize = [string  boundingRectWithSize:CGSizeMake(ScreenWidth-IPHONEWIDTH(56),MAXFLOAT ) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:IPHONEWIDTH(30)]} context:NULL].size;

CGFloat height = contentSize.height;

//进行返回
NSArray * array =@[strAttebute,[NSString stringWithFormat:@"%0.2f",contentSize.width],[NSString stringWithFormat:@"%0.2f",height]];

return array;

}

@end


案例1:修改文本字体大小、颜色属性

比如文本展示为姓名和性别,但是我们不能排除姓名会很长,所以此刻的lable宽度我们就不能写死,换句话说lable的宽度根据文本的内容来定



我经常用两种方式解决:

1.前面文章已经涉及:lable自适应http://blog.csdn.net/tuwanli125/article/details/51003798

2.就是使用NSMutableAttributedString属性给infoL设置文本

NSString *infoStr = [NSStringstringWithFormat:@"%@ %@",name,sex];

NSMutableAttributedString *infoAttStr = [[NSMutableAttributedStringalloc] initWithString:infoStr];

NSArray *colorArr =@[[UIColorcolorWithRed:0/255.0green:168/255.0blue:255/255.0alpha:1.0],[UIColorcolorWithRed:153/255.0green:153/255.0blue:153/255.0alpha:1.0]];

--------修改姓名的颜色,字体大小------

[infoAttStr addAttribute:NSForegroundColorAttributeNamevalue:colorArr[0]range:NSMakeRange(0,name.length)];

[infoAttStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:15]range:NSMakeRange(0,name.length)];

--------修改性别的颜色,字体大小------

[infoAttStr addAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:12]range:NSMakeRange(name.length+1,sexStr.length)];

[infoAttStr addAttribute:NSForegroundColorAttributeNamevalue:colorArr[1]range:NSMakeRange(name.length+1,sexStr.length)];

[self.infoL setAttributedText:infoAttStr];

这样一个文本就可以了,简单快捷

案例2:文本行间距

remindLabel.text = @""(一堆文字,此处省略一万字

)

NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc]initWithString:remindLabel.text];;

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init];

[paragraphStyle setLineSpacing:9];

paragraphStyle.maximumLineHeight = 60; //最大的行高

[paragraphStyle setFirstLineHeadIndent:30];//首行缩进

[attributedString addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, remindLabel.text.length)];

remindLabel.attributedText = attributedString;

文本行间距 以及自定义高度

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81 }
span.s1 { color: #000000 }
span.s2 { }
span.s3 { color: #3e1e81 }
span.s4 { color: #008400 }
span.s5 { font: 11.0px "PingFang SC"; color: #008400 }
span.s6 { color: #703daa }
span.s7 { color: #78492a }
span.s8 { color: #272ad8 }
span.s9 { color: #4f8187 }
span.s10 { color: #ba2da2 }
NSMutableAttributedString * strAttebute = [[NSMutableAttributedString alloc] initWithString:check1];

//设置行间距

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

[paragraphStlyle setLineSpacing:IPHONEHIGHT(10)];

[strAttebute addAttribute:NSParagraphStyleAttributeName value:paragraphStlyle range:NSMakeRange(0, check1.length)];

c2ell.labelText.attributedText = strAttebute;

CGSize contentSize = [c2ell.labelText.text boundingRectWithSize:CGSizeMake(ScreenWidth-IPHONEWIDTH(28*3+140),MAXFLOAT ) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:IPHONEWIDTH(30)],NSParagraphStyleAttributeName:paragraphStlyle} context:NULL].size;

c2ell.labelText.size =CGSizeMake(contentSize.width, contentSize.height);

案例3:添加下划线

我给按钮添加下滑线,比如按钮显示文本为电话号码,点击就可以拨打电话



NSMutableAttributedString *str = [[NSMutableAttributedStringalloc]initWithString:_phoneBtn.titleLabel.text];

NSRange strRange = {0,[strlength]};

[str addAttribute:NSUnderlineStyleAttributeNamevalue:[NSNumbernumberWithInteger:NSUnderlineStyleSingle]range:strRange];

[_phoneBtnsetAttributedTitle:strforState:UIControlStateNormal]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐