如何在IOS中以编程方式创建TTTAttributedLabel对齐中心
我正在开发一个应用程序,其中我有一个字符串,其中包含前缀为#的标签。
I am developing an app where I have a string that consists of tags with prefix #.
我使用TTTAttribute标签添加带有前缀的字词的链接#在给定的字符串中。
I am using TTTAttribute Label to add links to words which are having prefix # in a given string.
当我添加到TTTAttribute标签的链接时。它已成功添加,当点击它时,我能够在该字符串中获得具有前缀#的所选单词..
When I added links to TTTAttribute label. It successfully added and when clicking on that I am able to get that selected word having prefix # in that string..
但我无法将TTTAttribute标签对齐中心基于字符串长度..
But I was not able to align center the TTTAttribute label based on string length..
默认属性
attributedLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter;
在申请链接时无效。我希望标签对齐中心的长度如下所示..
is not working while applying links..I want the label align center based on its length as shown below..
如果是正常的TTTAttribute标签而未应用链接,则默认的对齐属性正确应用。 。
If it is normal TTTAttribute label without applying links then default align property is applying correctly..
这是我用来添加链接的代码..
Here is the code I used for adding links..
- (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标签的框架大小..
I don't want to resize the frames of TTTAttribute label..
任何建议或帮助将不胜感激..
Any suggestions or help will be appreciated..
在此先感谢..
非常简单,请使用以下代码:
Its very simple, use the following code:
attributedLabel.textAlignment = NSTextAlignmentCenter;