接頭辞 # が付いたタグで構成される文字列を持つアプリを開発しています。
TTTAttribute Label を使用して、特定の文字列にプレフィックス # を持つ単語へのリンクを追加しています。
リンクを TTTAttribute ラベルに追加したとき。正常に追加され、それをクリックすると、その文字列にプレフィックス # を持つ選択した単語を取得できます..
しかし、文字列の長さに基づいて TTTAttribute ラベルを中央に揃えることができませんでした..
デフォルトのプロパティ
attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter;
リンクの適用中に機能しません..以下に示すように、ラベルの長さに基づいて中央に配置したい..
リンクを適用しない通常の 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 ラベルのフレームのサイズを変更したくありません..
任意の提案やヘルプをいただければ幸いです..
前もって感謝します..