我有一个使用编程自动布局创建的 UILabel 对象 calloutLabel :
- (void)setupCalloutLabel
{
UILabel *calloutLabel = [UILabel newAutoLayoutView];
calloutLabel.backgroundColor = [UIColor clearColor];
calloutLabel.textAlignment = NSTextAlignmentCenter;
calloutLabel.numberOfLines = 2;
_calloutLabel = calloutLabel;
[self addSubview:calloutLabel];
NSNumber *padding = @(10.0f);
NSNumber *calloutLabelHeight = @(132.0f);
NSDictionary *calloutLabelMetrics = NSDictionaryOfVariableBindings(padding, calloutLabelHeight);
UIImageView *logoImageView = _logoImageView;
NSDictionary *ecalloutLabelViews = NSDictionaryOfVariableBindings(calloutLabel, logoImageView);
NSLayoutConstraint *calloutLabelHorizontalConstraint = [NSLayoutConstraint constraintWithItem:calloutLabel
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0.0f];
NSArray *calloutLabelVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat"V:[logoImageView]-(padding)-[calloutLabel(calloutLabelHeight)]"
options:NSLayoutFormatAlignAllCenterX
metrics:calloutLabelMetrics
views:ecalloutLabelViews];
[self addConstraint:calloutLabelHorizontalConstraint];
[self addConstraints:calloutLabelVerticalConstraints];
[self setNeedsLayout];
[self layoutIfNeeded];
}
在后面的代码中,我用以下内容动态更新了 _calloutLabel.attributedText :
NSString *calloutString = offer[kAPIAttributeOffersOfferCallout];
NSMutableAttributedString *calloutAttributedString = [[NSMutableAttributedString alloc] initWithString:calloutString];
NSString *firstLine = nil;
NSScanner *calloutScanner = [NSScanner scannerWithString:calloutString];
[calloutScanner scanUpToString"\n" intoString:&firstLine];
NSRange firstLineRange = NSRangeFromString(firstLine);
NSDictionary *firstLineAttributes = @{NSFontAttributeName : [UIFont fontWithName:kFontNameSemibold size:48.0f],
NSForegroundColorAttributeName : [UIColor colorWithHex:kColorLightBlue andAlpha:1.0f]};
[calloutAttributedString addAttributes:firstLineAttributes range:firstLineRange];
NSString *secondLine = nil;
[calloutScanner scanUpToString"" intoString:&secondLine];
NSRange secondLineRange = NSRangeFromString(secondLine);
NSDictionary *secondLineAttributes = @{NSFontAttributeName :[UIFont fontWithName:kFontNameSemibold size:30.0f],
NSForegroundColorAttributeName : [UIColor colorWithHex:kColorLightBlue andAlpha:1.0f]};
[calloutAttributedString addAttributes:secondLineAttributes range:secondLineRange];
_calloutLabel.attributedText = calloutAttributedString;
它应该是这样的:
实际上是这样的:
我是不是在某个地方犯了错误?
谢谢!
Best Answer-推荐答案 strong>
NSRangeFromString 用于从它的字符串表示中创建一个范围(例如@“{1,5}”),而不是获取扫描字符串的范围。
您真正想要做的是将范围设置为扫描仪位置,所以
NSRange firstLineRange = NSMakeRange(0, calloutScanner.scanLocation);
对于第二个位置,您可以跳过扫描,只需使用字符串的长度
NSRange secondLineRange = NSMakeRange(firstLineRange.length + 1, [calloutString length] - firstLineRange.length - 1);
关于ios - 使用自动布局创建的 UILabel 不显示 NSMutableAttributedString 的属性,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/27005774/
|