I am developing an app where I have a string that consists of tags with prefix #.
I am using TTTAttribute Label to add links to words which are having prefix # in a given string.
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..
But I was not able to align center the TTTAttribute label based on string length..
The default property
attributedLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentCenter;
is not working while applying links..I want the label align center based on its length as shown below..
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);
}
I don't want to resize the frames of TTTAttribute label..
Any suggestions or help will be appreciated..
Thanks in Advance..
You can also change the label text alignment by change the UILabel object’s textAlignment property value t0 NSTextAlignment.left, NSTextAlignment.center or NSTextAlignment.right. You can also change the label text wrap method by setting the label object’s lineBreakMode property.
// Initialize the tips message label. // Set message label's initial text alignment to center. // Add the two labels to the screen. /* This function will be invoked when click button 'Change Label Text Alignment'.
// Initialize the tips message label. // Set message label's initial text alignment to center. // Add the two labels to the screen. /* This function will be invoked when click button 'Change Label Text Alignment'. */ labelOne.text = "Hello swift label."
When you click the first ‘ Change Label Text Alignment ‘ button, it will change the label text alignment value in NSTextAlignment.left, NSTextAlignment.center, NSTextAlignment.right order. You can see the current label text alignment value in the message label tips below the blue label.
You need to set the link attributes with the paragraph style set:
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];
Its very simple, use the following code:
attributedLabel.textAlignment = NSTextAlignmentCenter;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With