2

我有一个 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;

它应该是这样的: 在此处输入图像描述

它实际上是这样的: 在此处输入图像描述

我在某个地方犯了错误吗?

谢谢!

4

1 回答 1

1

NSRangeFromString 用于从它的字符串表示中创建一个范围(例如@“{1,5}”),而不是获取扫描字符串的范围。

您真正想要做的是将范围扩大到扫描仪位置,所以

NSRange firstLineRange = NSMakeRange(0, calloutScanner.scanLocation);

对于第二个位置,您可能会跳过扫描,只需使用字符串的长度

NSRange secondLineRange = NSMakeRange(firstLineRange.length + 1, [calloutString length] - firstLineRange.length - 1);
于 2014-11-18T23:14:56.647 回答