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
是一个错误。