UILable文本属性设置
2017-03-22 15:57
162 查看
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:@"NSAttributeString
可以用来设置字体、段落样式,字体颜色,字体背景颜色,可以添加删除线、下划线,可以设置字间距、阴影、空心字、斜体、扁平化"];
[attributedString
addAttribute:NSExpansionAttributeName
value:@1
range:NSMakeRange(0,
17)]; //
扁平化
[attributedString
addAttribute:NSObliquenessAttributeName
value:@1
range:NSMakeRange(18,
8)];//
倾斜
//段落
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
paragraphStyle.firstLineHeadIndent =
80;//
首行缩进
paragraphStyle.headIndent =
25; //
其它行缩进
paragraphStyle.lineSpacing =
20; //
行间距
[attributedString
addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, attributedString.length)];//
段落
NSShadow *shadow = [[NSShadow
alloc] init];
shadow.shadowBlurRadius =
5; //模糊度
shadow.shadowColor = [UIColor
redColor];
shadow.shadowOffset =
CGSizeMake(1,
3);
[attributedString
addAttribute:NSVerticalGlyphFormAttributeName
value:@(0)
range:NSMakeRange(27,
4)];
[attributedString
addAttribute:NSShadowAttributeName
value:shadow range:NSMakeRange(27,
4)];
[attributedString
addAttribute:NSStrokeWidthAttributeName
value:@(-3.0)
range:NSMakeRange(32,
11)];//
边线宽度
[attributedString
addAttribute:NSStrokeColorAttributeName
value:[UIColor
greenColor] range:NSMakeRange(32,
11)];//边线颜色,需要先设置边线宽度
[attributedString
addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(44,
7)]; //
删除线
[attributedStri
4000
ng
addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(52,
3)]; //
下划线
[attributedString
setAttributes:@{NSFontAttributeName: [UIFont
fontWithName:@"Arial-BoldItalicMT"
size:18],
NSKernAttributeName:@(10),NSForegroundColorAttributeName:[UIColor
redColor], NSBackgroundColorAttributeName:[UIColor
yellowColor]}
range:NSMakeRange(56,
20)];
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
0, self.view.bounds.size.width,
200)];
label.numberOfLines =
0;
label.backgroundColor = [UIColor
grayColor];
label.attributedText = attributedString;
[self.view
addSubview:label];
#pragma mark
顶上
- (void)alignTop;
/**
* 改变行间距
*/
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变字间距
*/
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变行间距和字间距
*/
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;
- (void)alignTop {
CGSize fontSize = [self.text
sizeWithFont:[UIFont
systemFontOfSize:19.0
weight:0.2]];
double finalHeight = fontSize.height *
self.numberOfLines;
double finalWidth =
self.frame.size.width;
//expected width of label
CGSize theStringSize = [self.text
sizeWithFont:[UIFont
systemFontOfSize:19.0
weight:0.2]
constrainedToSize:CGSizeMake(finalWidth, finalHeight)
lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text = [self.text
stringByAppendingString:@"\n "];
}
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText
attributes:@{NSKernAttributeName:@(space)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace
{
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText
attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
alloc] initWithString:@"NSAttributeString
可以用来设置字体、段落样式,字体颜色,字体背景颜色,可以添加删除线、下划线,可以设置字间距、阴影、空心字、斜体、扁平化"];
[attributedString
addAttribute:NSExpansionAttributeName
value:@1
range:NSMakeRange(0,
17)]; //
扁平化
[attributedString
addAttribute:NSObliquenessAttributeName
value:@1
range:NSMakeRange(18,
8)];//
倾斜
//段落
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
paragraphStyle.firstLineHeadIndent =
80;//
首行缩进
paragraphStyle.headIndent =
25; //
其它行缩进
paragraphStyle.lineSpacing =
20; //
行间距
[attributedString
addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, attributedString.length)];//
段落
NSShadow *shadow = [[NSShadow
alloc] init];
shadow.shadowBlurRadius =
5; //模糊度
shadow.shadowColor = [UIColor
redColor];
shadow.shadowOffset =
CGSizeMake(1,
3);
[attributedString
addAttribute:NSVerticalGlyphFormAttributeName
value:@(0)
range:NSMakeRange(27,
4)];
[attributedString
addAttribute:NSShadowAttributeName
value:shadow range:NSMakeRange(27,
4)];
[attributedString
addAttribute:NSStrokeWidthAttributeName
value:@(-3.0)
range:NSMakeRange(32,
11)];//
边线宽度
[attributedString
addAttribute:NSStrokeColorAttributeName
value:[UIColor
greenColor] range:NSMakeRange(32,
11)];//边线颜色,需要先设置边线宽度
[attributedString
addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(44,
7)]; //
删除线
[attributedStri
4000
ng
addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:NSMakeRange(52,
3)]; //
下划线
[attributedString
setAttributes:@{NSFontAttributeName: [UIFont
fontWithName:@"Arial-BoldItalicMT"
size:18],
NSKernAttributeName:@(10),NSForegroundColorAttributeName:[UIColor
redColor], NSBackgroundColorAttributeName:[UIColor
yellowColor]}
range:NSMakeRange(56,
20)];
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
0, self.view.bounds.size.width,
200)];
label.numberOfLines =
0;
label.backgroundColor = [UIColor
grayColor];
label.attributedText = attributedString;
[self.view
addSubview:label];
#pragma mark
顶上
- (void)alignTop;
/**
* 改变行间距
*/
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变字间距
*/
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;
/**
* 改变行间距和字间距
*/
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;
- (void)alignTop {
CGSize fontSize = [self.text
sizeWithFont:[UIFont
systemFontOfSize:19.0
weight:0.2]];
double finalHeight = fontSize.height *
self.numberOfLines;
double finalWidth =
self.frame.size.width;
//expected width of label
CGSize theStringSize = [self.text
sizeWithFont:[UIFont
systemFontOfSize:19.0
weight:0.2]
constrainedToSize:CGSizeMake(finalWidth, finalHeight)
lineBreakMode:self.lineBreakMode];
int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text = [self.text
stringByAppendingString:@"\n "];
}
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[paragraphStyle setLineSpacing:space];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText
attributes:@{NSKernAttributeName:@(space)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace
{
NSString *labelText = label.text;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString
alloc] initWithString:labelText
attributes:@{NSKernAttributeName:@(wordSpace)}];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle
alloc] init];
[paragraphStyle setLineSpacing:lineSpace];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle range:NSMakeRange(0, [labelText
length])];
label.attributedText = attributedString;
[label sizeToFit];
}
相关文章推荐
- TortoiseSVN - 项目属性 - tsvn:logwidthmarker属性设置文本宽度指示
- 在可编辑的(contentEditable="true")的DIV中动态设置文本属性
- 设置导航条上文本内容的属性
- 如何设置UILable的各种属性
- [R语言绘图]绘图样式设置(符号、线条、颜色、文本属性)
- VB实现文本打印 打印机属性设置[转]
- UILable属性详解,设置居上对齐,居中对齐,居下对齐,获取斜体字,字体、大小、单位、颜色
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性
- UILable 的 属性设置
- DuiDesigner控件Label无法设置文本对齐属性的BUG解决方案
- Flex中如何通过displayAsPassword属性,设置TextInput文本显示密码形式(*)
- winform窗体中 ComboBox 控件的DropDownWidth属性根据显示的文本设置下拉列表的显示宽度
- 通过代码创建UILable,并设置平移旋转字体颜色等属性
- 【Android常用控件】EditText常用属性【二】:为文本输入框指定软键盘类型和软键盘回车键图标设置
- font-weight 属性设置文本的粗细
- 怎么设置文本属性为只读和隐藏 C#里面采用FileSream写入一个txt文本
- android文本组件textView属性autolink设置为web,显示网址的处理方法
- android:TextView中的文本链接之--设置超链接、颜色、字体等属性
- UIlable 属性设置 全
- 设置UILable文本的粗体其他字体