NSAttributedString换行

常用属性:

NSFontAttributeName           文字字体

NSParagraphStyleAttributeName     段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

 [段落样式-插曲]
1
@property(readwrite) CGFloat lineSpacing;              //行间距 2 @property(readwrite) CGFloat paragraphSpacing;           //段间距 3 @property(readwrite) NSTextAlignment alignment;           //对齐方式 4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧 5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进 6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度 7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式 8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高 9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高 10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNaturalNSWritingDirectionLeftToRightNSWritingDirectionRightToLeft
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
 [段落样式demo]
1
UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)]; 2 lable.backgroundColor = [UIColor lightGrayColor]; 3 lable.numberOfLines = 0; 4 [self.view addSubview:lable]; 5 6 NSString * string = @"Always believe that something wonderful is about \nto happen!"; 7 8 //富文本 9 NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string]; 10 11 //段落样式 12 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 13 14 #warning lable.numberOfLines必须为0,段落样式才生效 15 //行间距 16 paragraphStyle.lineSpacing = 10.0; 17 //段落间距 18 paragraphStyle.paragraphSpacing = 20.0; 19 20 // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; 21 // paragraphStyle.firstLineHeadIndent = 10.0; 22 // paragraphStyle.headIndent = 50.0; 23 // paragraphStyle.tailIndent = 200.0; 24 25 [attributedString addAttribute:NSParagraphStyleAttributeName 26 value:paragraphStyle 27 range:NSMakeRange(0, string.length)]; 28 29 lable.attributedText = attributedString;
技术分享

你可能感兴趣的:(IOS-UI基础)