300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ios label 高度紫石英_iOS_NSMutableAttributedString和自适应宽度高度

ios label 高度紫石英_iOS_NSMutableAttributedString和自适应宽度高度

时间:2020-12-11 20:15:16

相关推荐

ios label 高度紫石英_iOS_NSMutableAttributedString和自适应宽度高度

在此之前我们应该知道NSAttributedString,NSMutableAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。它是实现富文本的不二之选。​

首先我们创建的时候可以看到很多方法,其实最常用的也就这三个。而我一般都只是用到了第一个。​

-

(instancetype)initWithString:(NSString

*)str;

-

(instancetype)initWithString:(NSString

*)str attributes:(nullable

NSDictionary<

se-mark="1">NSString *,

id> *)attrs;​

-

(instancetype)initWithAttributedString:(NSAttributedString

*)attrStr;

下面这两个也是最常用的,给某一段文本添加属性。​

-

(void)addAttribute:(NSString *)name

value:(id)value range:(NSRange)range;

-

(void)addAttributes:(NSDictionary<

se-mark="1">NSString *,

id> *)attrs

range:(NSRange)range;

方法都很简单,就不详细说明了。

下面我们就来编写代码,这里以我很喜欢的一首歌的歌词为例:

UIScrollView *scrollView =

[[UIScrollView

alloc]initWithFrame:self.view.frame];

[self.view

addSubview:scrollView];

UILabel

*label = [[UILabel

alloc]init];

label.textAlignment =

NSTextAlignmentCenter;

label.font

=

[UIFontsystemFontOfSize:16];

label.numberOfLines = 0;

[scrollView

addSubview:label];

NSString *labelText =

@"\n\n\n\n尚好的青春\n词:潘协庆\n曲:郭文贤\n演唱:孙燕姿\n\n尚好的青春都是你\n再遥远都跟随你\n若滂沱大雨不曾见证海角相偎依\n衣角怎么会湿淋淋\n\n尚好的青春都是你\n没有片刻不想你\n就算能真在对的时间遇见对的你\n遗失的青春怎能回得去\n\n千万记得天涯有人在等你\n风再疾再狂我也不放弃\n愿为你直到有一刻能守着你的心\n就算你不会懂也不会可惜\n\n千万记得天涯有人在等待\n路程再多遥远不要不回来\n不去想不去计量你的心有多明白\n前往幸福的路有多少阻碍\n就算给你爱石沉大海\n青春飞逝就再找不回来\n\n\n赤橙黄绿青蓝紫";

[selfsetColourfulTextWithLabel:label

labelText:labelText];

// 单行文本计算宽高

// CGSize size =[labelText

sizeWithAttributes:@{NSFontAttributeName:[UIFont

systemFontOfSize:16]}];

// 多行文本计算宽高

CGSize

size = [labelText

boundingRectWithSize:CGSizeMake(self.view.frame.size.width,

MAXFLOAT)options:NSStringDrawingUsesLineFragmentOrigin

attributes:@{NSFontAttributeName:[UIFont

systemFontOfSize:16]}context:nil].size;

label.frame

= CGRectMake(0,

0,

self.view.frame.size.width,

size.height +

10);

scrollView.contentSize =

CGSizeMake(0, size.height +

10);

好丑有木有!!!

现在我们让​NSMutableAttributedString工作起来,使它看起来真正像歌单一点。

// 给标签设置五彩缤纷的文字

-

(void)setColourfulTextWithLabel:(UILabel

*)label labelText:(NSString *)labelText {

NSMutableAttributedString *attributedString =

[[NSMutableAttributedString

alloc]initWithString:labelText];

[attributedString

addAttribute:NSFontAttributeName

value:[UIFont

boldSystemFontOfSize:20]

range:NSMakeRange(4,

5)];

[attributedString

addAttribute:NSFontAttributeName

value:[UIFont

systemFontOfSize:14]

range:NSMakeRange(9,

19)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(4,

5)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(labelText.length

- 7, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:COLORRGB(255,

128, 0)

range:NSMakeRange(labelText.length

- 6, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:COLORRGB(255,

255, 0)

range:NSMakeRange(labelText.length

- 5, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:[UIColor

greenColor]

range:NSMakeRange(labelText.length

- 4, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:COLORRGB(0,

255, 255)

range:NSMakeRange(labelText.length

- 3, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:[UIColor

blueColor]

range:NSMakeRange(labelText.length

- 2, 1)];

[attributedString

addAttribute:NSForegroundColorAttributeName

value:COLORRGB(128,

0, 255)

range:NSMakeRange(labelText.length

- 1, 1)];

label.attributedText = attributedString;

}

咯,现在漂亮多了吧~

下面附上 Attributes

的所有属性,方便小伙伴儿们愉快的搬砖~

//

值为UIFont,设置字体,默认值为12-point

Helvetica(Neue).

NSString *

const NSFontAttributeName;

//值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle

defaultParagraphStyle]返回的值。

NSString *

const NSParagraphStyleAttributeName;

//

值为UIColor,字体颜色,默认为黑色。

NSString *

const NSForegroundColorAttributeName;

//

值为UIColor,字体背景色,默认没有。

NSString *

const NSBackgroundColorAttributeName;

//

为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios

上不支持。

NSString *

const NSLigatureAttributeName;

//

值为浮点数NSNumber,字距属性,默认值为0。

NSString *

const NSKernAttributeName;

// 值为整型NSNumber,可取值为

NSUnderlineStyleNone = 0×00 和

NSUnderlineStyleSingle = 0×01

设置删除线。

NSString *

const NSStrikethroughStyleAttributeName;

// 同上。设置下划线

NSString *

const NSUnderlineStyleAttributeName;

//

值为UIColor,默认值为nil,设置的属性同ForegroundColor。

NSString *

const NSStrokeColorAttributeName;

//

值为浮点数NSNumber。设置比画的粗细。

NSString *

const NSStrokeWidthAttributeName;

//

值为NSShadow,设置比画的阴影,默认值为nil。

NSString *

const NSShadowAttributeName;

// 设置文本特殊效果,取值为

NSString 对象,(图版印刷效果)

NSString *

const NSTextEffectAttributeName;

//

文本附件,取值为NSTextAttachment对象,常用于文字图片混排

NSString *

const NSAttachmentAttributeName;

//

设置链接属性,点击后调用浏览器打开指定URL地址

//

如果一个label设置了这个属性,那它其他的设置都将失效。

NSString *

const NSLinkAttributeName;

// 设置基线偏移值,取值为 NSNumber

(float),正值上偏,负值下偏

NSString *

const NSBaselineOffsetAttributeName;

// 设置下划线颜色

NSString *

const NSUnderlineColorAttributeName;

// 设置删除线颜色

NSString *

const NSStrikethroughColorAttributeName;

// 设置字形倾斜度,取值为 NSNumber

(float),正值右倾,负值左倾

NSString *

const NSObliquenessAttributeName;

// 文本横向拉伸属性,取值为

NSNumber

(float),正值横向拉伸文本,负值横向压缩文本

NSString *

const NSExpansionAttributeName;

// 设置文字书写方向

NSString *

const NSWritingDirectionAttributeName;

//

值为整型NSNumber,0为水平排版的字,1为垂直排版的字。

NSString *

const NSVerticalGlyphFormAttributeName;

最后再送给小伙伴一个小福利~

// 查看系统支持字体

- (void)lookSystemFont {

NSArray

*familyArray = [UIFont familyNames];

for

(id family in familyArray) {

NSLog(@"family

==== %s\n",[family

cStringUsingEncoding:NSUTF8StringEncoding]);

NSArray *fontArray = [UIFont

fontNamesForFamilyName:family];

for (id font

in fontArray) {

NSLog(@"font ====

%s\n",[fontcStringUsingEncoding:NSUTF8StringEncoding]);

}

}

}

​能力技术有限,欢迎交流吐槽~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。