2

calloutLabelプログラムによる自動レイアウトを使用して作成された UILabel オブジェクトがあります。

- (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);

2 番目の場所については、文字列の長さだけを使用して、おそらくスキャンをスキップできます。

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