如何从 NSMutableAttributeString 获取 NSParagraphStyleAttributeName?
How to get NSParagraphStyleAttributeName from NSMutableAttributeString?
我创建了一个 NSMutableAttributedString
对齐方式如下:
UIFont * font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
/// Set text alignment
paragraphStyle.alignment = alignmentForString(textString) ;//NSTextAlignmentLeft;//NSTextAlignmentRight;// NSTextAlignmentNatural;// NSTextAlignmentLeft;
NSDictionary *messageDrawRectAttributesDictionary = @{
NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName:[UIColor blackColor]
};
NSMutableAttributedString * mutalbleAttributedString = [[NSMutableAttributedString alloc] initWithString:textString attributes:messageDrawRectAttributesDictionary];
我想知道(以后在应用程序中使用)
如何获得我在此字符串中设置的对齐方式。
我试过这段代码,但没有完全理解我在做什么:
NSRange range = NSMakeRange(0,mutalbleAttributedString.length-1);
NSDictionary * paraDic =[mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:&range inRange:range];
现在,我搜索了很多示例代码并阅读了 Apple 文档。但没有运气。我得到空字典。
我唯一需要的帮助是,如果有人可以编写正确的代码,将 return 比对数据给我。
当您调用 attribute:atIndex:longestEffectiveRange:inRange:
时,return 值是所请求属性的值,而不是字典。另外,不要为两个范围参数传入 range
。
你的代码应该是这样的:
NSRange range = NSMakeRange(0, mutalbleAttributedString.length);
NSParagraphStyle *paraStyle = [mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:NULL inRange:range];
NSTextAlignment alignment = paraStyle.alignment;
注意以下变化:
- 所请求属性的正确 return 值
- 为
range
设置合适的长度
- 将
NULL
传递给 longestEffectiveRange:
,因为您不关心该值。
您可以像这样从 NSMutableAttributedString 中获取对齐方式:
NSRange range = NSMakeRange(0, mutableAttributedString.length);
NSDictionary *attributes = [mutableAttributedString attributesAtIndex:0 effectiveRange:&range];
NSMutableParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
NSTextAlignment alignment = paragraphStyle.alignment;
Swift 5 示例:
var effectiveRange = NSMakeRange(0, attributedString.length)
let currentParagraphStyle: NSParagraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: &effectiveRange) as! NSParagraphStyle
let align = currentParagraphStyle.alignment
let lineSpacing = currentParagraphStyle.lineSpacing
let newParagraphStyle = currentParagraphStyle.lineHeightMultiple
我创建了一个 NSMutableAttributedString
对齐方式如下:
UIFont * font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
/// Make a copy of the default paragraph style
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
/// Set line break mode
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
/// Set text alignment
paragraphStyle.alignment = alignmentForString(textString) ;//NSTextAlignmentLeft;//NSTextAlignmentRight;// NSTextAlignmentNatural;// NSTextAlignmentLeft;
NSDictionary *messageDrawRectAttributesDictionary = @{
NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName:[UIColor blackColor]
};
NSMutableAttributedString * mutalbleAttributedString = [[NSMutableAttributedString alloc] initWithString:textString attributes:messageDrawRectAttributesDictionary];
我想知道(以后在应用程序中使用) 如何获得我在此字符串中设置的对齐方式。
我试过这段代码,但没有完全理解我在做什么:
NSRange range = NSMakeRange(0,mutalbleAttributedString.length-1);
NSDictionary * paraDic =[mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:&range inRange:range];
现在,我搜索了很多示例代码并阅读了 Apple 文档。但没有运气。我得到空字典。
我唯一需要的帮助是,如果有人可以编写正确的代码,将 return 比对数据给我。
当您调用 attribute:atIndex:longestEffectiveRange:inRange:
时,return 值是所请求属性的值,而不是字典。另外,不要为两个范围参数传入 range
。
你的代码应该是这样的:
NSRange range = NSMakeRange(0, mutalbleAttributedString.length);
NSParagraphStyle *paraStyle = [mutalbleAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 longestEffectiveRange:NULL inRange:range];
NSTextAlignment alignment = paraStyle.alignment;
注意以下变化:
- 所请求属性的正确 return 值
- 为
range
设置合适的长度
- 将
NULL
传递给longestEffectiveRange:
,因为您不关心该值。
您可以像这样从 NSMutableAttributedString 中获取对齐方式:
NSRange range = NSMakeRange(0, mutableAttributedString.length);
NSDictionary *attributes = [mutableAttributedString attributesAtIndex:0 effectiveRange:&range];
NSMutableParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
NSTextAlignment alignment = paragraphStyle.alignment;
Swift 5 示例:
var effectiveRange = NSMakeRange(0, attributedString.length)
let currentParagraphStyle: NSParagraphStyle = attributedText?.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: &effectiveRange) as! NSParagraphStyle
let align = currentParagraphStyle.alignment
let lineSpacing = currentParagraphStyle.lineSpacing
let newParagraphStyle = currentParagraphStyle.lineHeightMultiple