- 下面是和好友@by小杰整理的一些內(nèi)容
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
NSLineBreakByWordWrapping = 0,
NSLineBreakByCharWrapping,
NSLineBreakByClipping, //減掉后面顯示不了的部分
NSLineBreakByTruncatingHead, //頭部內(nèi)容以......方式省略
NSLineBreakByTruncatingTail, //尾部內(nèi)容以......方式省略
NSLineBreakByTruncatingMiddle //中間內(nèi)容以......方式省略
} NS_ENUM_AVAILABLE_IOS(6_0);```
typedef NS_ENUM(NSInteger, NSWritingDirection) {
NSWritingDirectionNatural = -1, // Determines direction using the Unicode Bidi Algorithm rules P2 and P3
NSWritingDirectionLeftToRight = 0, // Left to right writing direction 左到右的書寫方向
NSWritingDirectionRightToLeft = 1 // Right to left writing direction 右到左的書寫方向
} NS_ENUM_AVAILABLE_IOS(6_0);```
#段落風格
// NSParagraphStyleAttributeName 段落的風格(設(shè)置首行倚评,行間距,對齊方式什么的)看自己需要什么屬性秋忙,寫什么
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字體的行間距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行縮進
paragraphStyle.alignment = NSTextAlignmentJustified;//(兩端對齊的)文本對齊方式:(左认臊,中菠净,右,兩端對齊,自然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = 20;//整體縮進(首行除外)
paragraphStyle.tailIndent = 20;//
paragraphStyle.minimumLineHeight = 10;//最低行高
paragraphStyle.maximumLineHeight = 20;//最大行高
paragraphStyle.paragraphSpacing = 15;//段與段之間的間距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空間/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//從左到右的書寫方向(一共??三種)
paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = 1;//連字屬性 在iOS歼培,唯一支持的值分別為0和1
#字體大小
/* NSFontAttributeName 字體大小 NSParagraphStyleAttributeName 段落的風格(設(shè)置首行臭杰,行間距,對齊方式什么的) NSKernAttributeName 字間距 */
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle,
NSKernAttributeName:@(10),
};
#簡單應(yīng)用
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
實現(xiàn)效果:
實現(xiàn)代碼
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建富文本對象
NSMutableAttributedString *attrrbute1 = [[NSMutableAttributedString alloc] initWithString:self.label1.text];
//設(shè)置字體大小
[attrrbute1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:29] range:NSMakeRange(5, 4)];
//設(shè)置字體顏色
[attrrbute1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 4)];
//設(shè)置字體背景顏色
[attrrbute1 addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(15, 5)];
//設(shè)置刪除線
[attrrbute1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(31, 10)];
//設(shè)置下劃線
[attrrbute1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:2] range:NSMakeRange(40, 10)];
//一下兩者必須結(jié)合使用
//設(shè)置空心字顏色
[attrrbute1 addAttribute:NSStrokeColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(50, 10)];
//空心字邊框顏色
[attrrbute1 addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange(50, 10)];
//將富文本屬性添加到控件上
self.label1.attributedText = attrrbute1;
}
@end```
![UILabel.png](http://upload-images.jianshu.io/upload_images/1803308-6c0cfaa74775cc1f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```code
----- UILabel -----
//1 創(chuàng)建Label
UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
//2 設(shè)置屬性
//設(shè)置文字
namelabel.text = @"姓名";
//設(shè)置背景顏色
namelabel.backgroundColor = [UIColor whiteColor];
//設(shè)置內(nèi)容對齊方式
namelabel.textAlignment = NSTextAlignmentCenter;
//設(shè)置內(nèi)容字體大小
//namelabel.font = [UIFont systemFontOfSize:20];
namelabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
//設(shè)置文字超出, 換行顯示
namelabel.lineBreakMode = NSLineBreakByCharWrapping;
namelabel.numberOfLines = 0;
//設(shè)置Label圓角
namelabel.layer.borderWidth = 1;
namelabel.layer.cornerRadius = 8;
namelabel.layer.masksToBounds = YES;
//設(shè)置文字效果
//陰影效果
namelabel.shadowColor = [UIColor colorWithRed:0.203 green:0.260 blue:0.571 alpha:0.462];
//陰影位置
namelabel.shadowOffset = CGSizeMake(5, 5);// (x, y)
//設(shè)置文字角度
namelabel.transform = CGAffineTransformMakeRotation(0.2);
//添加富文本
//1 聲明富文本
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"富文本文字+ 測試換行文字文字文字文字文字文字"];
//2 設(shè)置富文本內(nèi)容樣式
//2.1 字體大小
//參數(shù)一: 修改的屬性 參數(shù)二: 類型的具體值 參數(shù)三: 修改的區(qū)間
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40] range:NSMakeRange(0, 3)];
//2.2 設(shè)置空心字
[attributeStr addAttribute:NSStrokeWidthAttributeName value:@2 range:NSMakeRange(0, 4)];
//2.3 設(shè)置空心字顏色
[attributeStr addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
//3 為label指定富文本對象(需要放置在富文本樣式后面)
namelabel.attributedText = attributeStr;
//namelabel.backgroundColor = [UIColor colorWithRed:0.792 green:1.000 blue:0.814 alpha:1.000];
//3 添加到視圖
[self.view addSubview:namelabel];