Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 获取UILabel的NSParagraphStyle_Objective C_Uilabel_Nsparagraphstyle - Fatal编程技术网

Objective c 获取UILabel的NSParagraphStyle

Objective c 获取UILabel的NSParagraphStyle,objective-c,uilabel,nsparagraphstyle,Objective C,Uilabel,Nsparagraphstyle,有没有办法获取UILabel的NSParagraphStyle而不是创建新实例并设置每个属性?您可以使用enumerateAttribute:inRange:options:usingBlock:在UILabel的attributeText属性上检索NSParagraphStyle对象: NSAttributedString *attributedString = myLabel.attributedText; [attributedString enumerateAttribute:NSPa

有没有办法获取
UILabel
NSParagraphStyle
而不是创建新实例并设置每个属性?

您可以使用
enumerateAttribute:inRange:options:usingBlock:
UILabel
attributeText
属性上检索
NSParagraphStyle
对象:

NSAttributedString *attributedString = myLabel.attributedText;

[attributedString enumerateAttribute:NSParagraphStyleAttributeName
                             inRange:NSMakeRange(0, attributedString.length)
                             options:0
                          usingBlock:^(id value, NSRange range, BOOL *stop) {

                       NSParagraphStyle *paragraphStyle = value; // Do what you want with paragraph
}];

代码没有经过测试(可能由于一些小错误而无法编译),但它应该会告诉您背后的想法。

如果它有
属性文本
,您可以枚举并检索找到的第一个属性。枚举什么?枚举AttributeText的
NSParagraphAttributeName
。枚举什么属性?
NSAttributeString*AttributeString=myLabel.AttributeText;[attributedString EnumeratAttribute:NSParagraphAttributeName inRange:NSMakeRange(0,[attributedString长度])选项:0使用块:^(id值,NSRange范围,布尔*停止){NSParagraph*段落=值;}]?由于某种原因,
始终为
nil
。另外,我最初想从没有
属性文本的
UILabel
中检索它。你为什么毫无理由地编辑我的代码?特别是对于不会引起问题的修改(点语法与“方括号语法”),并且您不能将
NSParagraphStyle
作为方法的参数,它是一个NSString,需要存在
NSParagraphStyleAttributeName
,而不是类名。因为
length
是一个属性,而不是一个方法,这样更具可读性。其他编辑是因为代码没有编译,而
NSParagraphStyle
是一个错误。