段落样式NSMutableParagraphStyle
2016-08-25 本文已影响4836人
lltree
defaultParagraphStyle(单例方法)
+ (NSParagraphStyle *)defaultParagraphStyle;
// This class method returns a shared and cached NSParagraphStyle instance with the default style settings, with same value as the result of [[NSParagraphStyle alloc] init].
// 该方法返回一个附带默认值的单例对象,默认值等价于[[NSParagraphStyle alloc] init]方式创建
lineSpacing (行间距)
lineHeightMultiple(行间距多少倍)
字体行间距,默认行与行之间为default个点的间距,如果小于0则无效,如果值大于0则在默认值的基础上再加上lineSpacing,文字之间总高度为即:(default + lineSpacing),注意该间距不管字体大小
@property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing;
@property(NS_NONATOMIC_IOSONLY) CGFloat lineHeightMultiple;
测试代码一:(设置行间距为负值)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat y = 0;
for (int i =0; i<80; i++) {
UIView *view =[UIView new];
[self.view addSubview:view];
view.backgroundColor = i%2?[UIColor blueColor]:[UIColor redColor];
view.frame = CGRectMake(0, y, 10, 10);
y+=10;
}
[self testLineSpacing];
}
//测试行间距
-(void)testLineSpacing{
self.testLable.layer.borderWidth = 1;
self.testLable.numberOfLines = 0;
NSString *testStr =[NSString stringWithFormat:@"S-----+he must have been 6 years old, this beautiful brown haired!"];
NSMutableParagraphStyle *paragraphStyle =[[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -10;//连字符
NSDictionary *attrDict =@{
NSFontAttributeName:[UIFont systemFontOfSize:20],//字体大小
NSParagraphStyleAttributeName:paragraphStyle,//样式
};
NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:attrDict];
self.testLable.attributedText = attrStr;
}
展示效果:
测试代码二:(设置行间距为正直)
//在原有的代码基础上只修改该值
paragraphStyle.lineSpacing = 30;//连字符
展示效果:可以看到大约占了4个位置
paragraphSpacing(段落间距)
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing;
alignment (对齐方式)
@property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment;
firstLineHeadIndent(每段的首行缩进)
该值对应屏幕上的点
@property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent;
headIndent(整段缩进)
该值对应屏幕上的点
@property(NS_NONATOMIC_IOSONLY) CGFloat headIndent;
tailIndent(右侧缩进或显示宽度)
可调整文字尾端的缩排距离。需要注意的是,这里指定的值可以当作文字显示的宽、而也可当作右边 padding 使用,依据输入的正负值而定,如果为正值则代表文字要显示的宽度,如果为负值则代表文字距离右侧缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent;
lineBreakMode(截断模式)
@property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode;
截断模式有以下几种样式:
// NSParagraphStyle
typedef NS_ENUM(NSInteger, NSLineBreakMode) {
// Wrap at word boundaries, default
//出现在单词边界时起作用,如果该单词不在能在一行里显示时,整体换行。此为段的默认值
NSLineBreakByWordWrapping = 0,
// Wrap at character boundaries
//当一行中最后一个位置的大小不能容纳一个字符时,才进行换行。
NSLineBreakByCharWrapping,
// Simply clip 打断不显示....
//超出画布边缘部份将被截除。
NSLineBreakByClipping,
//截除前面部份,只保留后面一行的数据。前部份以...代替
// Truncate at head of line: "...wxyz"
NSLineBreakByTruncatingHead,
// Truncate at tail of line: "abcd..."
//截除后面部份,只保留前面一行的数据,后部份以...代替。
NSLineBreakByTruncatingTail,
// Truncate middle of line: "ab...yz"
//在一行中显示段文字的前面和后面文字,中间文字使用...代替。
NSLineBreakByTruncatingMiddle
} NS_ENUM_AVAILABLE(10_0, 6_0);
minimumLineHeight(最小行高)
@property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight;
maximumLineHeight (最大行高)
@property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight;
baseWritingDirection (书写方向)
@property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection;
paragraphSpacingBefore(段落间距)
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore;
hyphenationFactor(连字符属性)
指定连字符门限,有效值在0~1.0之间 ,
@property(NS_NONATOMIC_IOSONLY) float hyphenationFactor;
测试代码一:
self.testLable.layer.borderWidth = 1;
self.testLable.numberOfLines = 0;
NSString *testStr =[NSString stringWithFormat:@"S-----+he must have been 6 years old, this beautiful brown haired!"];
NSMutableParagraphStyle *paragraphStyle =[[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;//截断模式
paragraphStyle.hyphenationFactor = 0.3;//连字符
NSDictionary *attrDict =@{
NSFontAttributeName:[UIFont systemFontOfSize:17],//字体大小
NSParagraphStyleAttributeName:paragraphStyle,//样式
};
NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:attrDict];
self.testLable.attributedText = attrStr;
展示效果:
测试代码一:
self.testLable.layer.borderWidth = 1;
self.testLable.numberOfLines = 0;
NSString *testStr =[NSString stringWithFormat:@"S-----+he must have been 6 years old, this beautiful brown haired!"];
NSMutableParagraphStyle *paragraphStyle =[[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;//截断模式
paragraphStyle.hyphenationFactor = 0.3;//连字符
NSDictionary *attrDict =@{
NSFontAttributeName:[UIFont systemFontOfSize:17],//字体大小
NSParagraphStyleAttributeName:paragraphStyle,//样式
};
NSAttributedString *attrStr =[[NSAttributedString alloc] initWithString:testStr attributes:attrDict];
self.testLable.attributedText = attrStr;
展示效果:
tabStops(制表符)
@property(null_resettable, copy, NS_NONATOMIC_IOSONLY) NSArray<NSTextTab *> *tabStops NS_AVAILABLE(10_0, 7_0);
allowsDefaultTighteningForTruncation(收缩字符间距允许截断)
@property(NS_NONATOMIC_IOSONLY) BOOL allowsDefaultTighteningForTruncation
NS_AVAILABLE(10_11, 9_0);