6

接頭辞 # が付いたタグで構成される文字列を持つアプリを開発しています。

TTTAttribute Label を使用して、特定の文字列にプレフィックス # を持つ単語へのリンクを追加しています。

リンクを TTTAttribute ラベルに追加したとき。正常に追加され、それをクリックすると、その文字列にプレフィックス # を持つ選択した単語を取得できます..

しかし、文字列の長さに基づいて TTTAttribute ラベルを中央に揃えることができませんでした..

デフォルトのプロパティ

attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter;

リンクの適用中に機能しません..以下に示すように、ラベルの長さに基づいて中央に配置したい..

単一のタグでラベルを付ける

2 つのタグを付けたラベル

3 つのタグを付けたラベル

リンクを適用しない通常の TTTAttribute ラベルの場合、デフォルトの align プロパティが正しく適用されています。

リンクを追加するために使用したコードは次のとおりです。

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSRange matchRange;
        NSString *tweet = @"#MYTWEET ,#tweet, #fashion #Share";

        NSScanner *scanner = [NSScanner scannerWithString:tweet];
        if (![scanner scanUpToString:@"#" intoString:nil]) {
            // there is no opening tag
        }
        NSString *result = nil;
        if (![scanner scanUpToString:@" " intoString:&result]) {
            // there is no closing tag
        }
        //@"theString is:%@",result);


        NSArray *words = [tweet componentsSeparatedByString:@" "];

      TTTAttributedLabel *attributedLabel=[[TTTAttributedLabel alloc]initWithFrame:CGRectMake(5, 200, 320, 40)];

      attributedLabel.textAlignment=NSTextAlignmentCenter;

        attributedLabel.text=tweet;

        words = [tweet componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",0123456789`~!@$%^&*()_-+=.></?;:'* "]];

        for (NSString *word in words)
        {
            if ([word hasPrefix:@"#"])
            {
                //@"word %@",word);

                // Colour your 'word' here
                 matchRange=[tweet rangeOfString:word];

                [attributedLabel addLinkToURL:[NSURL URLWithString:word] withRange:matchRange];

                [tagsarray addObject:word];
            }

        }

        attributedLabel.delegate=self;

       }

    - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
    {
       //@"result ==== %@",url);

        NSString *webString=[NSString stringWithFormat:@"%@",url];

        NSString *tagstring = [webString stringByReplacingOccurrencesOfString:@"#" withString:@""];
        NSLog(@"Tag String is:%@",tagstring);

        }

TTTAttribute ラベルのフレームのサイズを変更したくありません..

任意の提案やヘルプをいただければ幸いです..

前もって感謝します..

4

2 に答える 2

16

段落スタイル セットでリンク属性を設定する必要があります。

NSMutableParagraphStyle* attributeStyle = [[NSMutableParagraphStyle alloc] init];
attributeStyle.alignment = NSTextAlignmentCenter;

NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica Neue" size:11], NSForegroundColorAttributeName:[UIColor colorWithRed:0.324 green:0.0 blue:0.580 alpha:1.0], NSParagraphStyleAttributeName:attributeStyle};

[self.atributedLabel setLinkAttributes:attributes];
于 2014-02-14T18:31:34.610 に答える