4

基本的に、最終結果として次のようなボタンが必要です。

ここに画像の説明を入力

ボタンが 2 行にまたがっており、一番上の行のフォント サイズが大きくなっています。この図では、上が 38pt、下が 24pt です。

これが NSAttributedString のタスクになることは承知していますが、正しく行う方法がわかりません。ストーリーボードに、属性付きに設定されたテキスト (および改行がワードラップに設定された) を持つ UIButton があり、次に、viewDidLoad次の操作を行うアウトレットがあります。

UIFont *font = [UIFont systemFontOfSize:39.0];
UIFont *font2= [UIFont systemFontOfSize:17.0];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"625 WPM"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(string.length - 2, 2)];

self.button.titleLabel.attributedText = string;

基本的にはすべて大きなフォントを使用し、最後の数文字は小さくします。しかし、それは単に大胆に見えます。

私は何を間違っていますか?

4

2 に答える 2

4

titleLabelにはUIButton、スタンドアロンUILabelと同じ方法ではアクセスできません。UIButtonsetAttributedTitle:forState:メソッドを使用する必要があります。

編集:(行間質問への回答に追加)

この例では、属性はNSParagraphStyleAttributeNameで、値はpStyle

NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.lineSpacing = 8;

return @{
          NSParagraphStyleAttributeName : pStyle
          };
于 2013-10-23T20:53:59.003 に答える
1

テキストサイズと属性が異なる2つのラベルが必要なBarButtonItemでそれを行いました。

2 UILabel への強力な参照を設定してから、次のようにします。

-(void) initBottomBar
{
    UIColor *labelColor;

    if ([Utility getiOSVersion] == 7)
        labelColor = [UIColor colorWithRed:43.0f/255.0f green:41.0f/255.0f blue:81.0f/255.0f alpha:1.0f]; // grayblue color
    else
        labelColor = [UIColor whiteColor];

    labelStringCategory                             = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 300, 22)];
    labelStringCategory.backgroundColor             = [UIColor clearColor];
    labelStringCategory.textColor                   = labelColor;
    labelStringCategory.font                        = [UIFont systemFontOfSize:14];
    labelStringCategory.numberOfLines               = 1;
    labelStringCategory.textAlignment               = NSTextAlignmentLeft;
    labelStringCategory.adjustsFontSizeToFitWidth   = YES;
    labelStringCategory.text = @"First Row";

    changeStringCategory                            = [[UILabel alloc] initWithFrame:CGRectMake(0, 19, 300, 22)];
    changeStringCategory.backgroundColor            = [UIColor clearColor];
    changeStringCategory.textColor                  = labelColor;
    changeStringCategory.font                       = [UIFont systemFontOfSize:12];
    changeStringCategory.numberOfLines              = 1;
    changeStringCategory.textAlignment              = NSTextAlignmentLeft;
    changeStringCategory.adjustsFontSizeToFitWidth  = YES;
    changeStringCategory.text = @"Second row";

    UIButton *displayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    displayButton.frame = CGRectMake(0, 0, 300, 44);
    [displayButton addSubview:labelStringCategory];
    [displayButton addSubview:changeStringCategory];
    displayButton.showsTouchWhenHighlighted = YES;
    [displayButton addTarget:self action:@selector(displayCategorySelected:) forControlEvents:UIControlEventTouchDown];

    UIBarButtonItem *labelStringCategoryBarItem = [[UIBarButtonItem alloc] initWithCustomView:displayButton];

    //...

そして、両方のラベルに text または textattribute を個別に設定できます。たとえば、1 ラベルの部分文字列を太字に設定するには、次のようにします。

const CGFloat fontSize = 13;
        UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
        UIFont *regularFont = [UIFont systemFontOfSize:fontSize];
        //UIColor *foregroundColor = [UIColor whiteColor];

        // Create the attributes
        NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                               regularFont, NSFontAttributeName, nil];
        NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                  boldFont, NSFontAttributeName, nil];
        const NSRange range = NSMakeRange(iFrom,iTo - iFrom);

        // Create the attributed string (text + attributes)
        NSMutableAttributedString *attributedText =
        [[NSMutableAttributedString alloc] initWithString:aText
                                               attributes:attrs];
        [attributedText setAttributes:subAttrs range:range];

        // Set it in our UILabel and we are done!
        [aLabel setAttributedText:attributedText];

お役に立てれば!ミック

于 2013-10-23T21:05:04.473 に答える