【问题标题】:How to make TTTAttributedLabel align center programatically in IOS如何在 IOS 中以编程方式使 TTTAttributedLabel 对齐中心
【发布时间】:2013-12-03 11:45:16
【问题描述】:

我正在开发一个应用程序,其中有一个由带有前缀# 的标签组成的字符串。

我正在使用 TTTAttribute 标签将链接添加到给定字符串中带有前缀 # 的单词。

当我添加到 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 标签的框架..

任何建议或帮助将不胜感激..

提前致谢..

【问题讨论】:

    标签: ios objective-c tttattritubedlabel


    【解决方案1】:

    你需要设置段落样式集的链接属性:

    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];
    

    【讨论】:

    【解决方案2】:

    很简单,使用如下代码:

    attributedLabel.textAlignment = NSTextAlignmentCenter;
    

    【讨论】:

    • 你肯定在那里犯了一些错误。它对我有用,它已经过全面测试。请放一些代码或自己调试。
    • @Parcs..我在我的视图控制器中发布了整个代码。请发送您已完成的一些代码或示例链接...我试图解决这个问题这么多天..它需要发布我的应用程序...
    • 写下这段代码,你要在其中创建标签: TTTAttributedLabel *attributedLabel=[[TTTAttributedLabel alloc]initWithFrame:CGRectMake(5, 200, 320, 40)];属性标签.textAlignment = NSTextAlignmentCenter;
    • @Parcs....我添加了您的代码,但它没有对齐中心。如果我将 cmets 放在 [attributedLabel addLinkToURL:[NSURL URLWithString:word] withRange:matchRange];这段代码然后它的工作。为什么它的行为改变了我不知道......
    • 这实际上对我来说效果很好 - 不知道为什么其他人没有。
    猜你喜欢
    • 2018-12-15
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多