如何使 TTTAttributedLabel 以编程方式在 IOS 中的居中对齐
发布网友
发布时间:2022-08-14 11:30
我来回答
共2个回答
热心网友
时间:2024-01-01 17:17
默认属性
attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter ;
工作不同时应用链接...我想要基于它的长度,如下所示的居中对齐标签...
如果是正常的 TTTAttribute 标签而不应用的链接,然后默认对齐属性正确应用...
这里是我用于添加链接的代码...
- (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 标签...
将赞赏的任何建议或帮助...
先谢谢了...
解决方法 1:
它非常简单,使用下面的代码:
attributedLabel.textAlignment = NSTextAlignmentCenter;
热心网友
时间:2024-01-01 17:17
默认属性
attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter ;
工作不同时应用链接...我想要基于它的长度,如下所示的居中对齐标签...
如果是正常的 TTTAttribute 标签而不应用的链接,然后默认对齐属性正确应用...
这里是我用于添加链接的代码...
- (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 标签...
将赞赏的任何建议或帮助...
先谢谢了...
解决方法 1:
它非常简单,使用下面的代码。