TTStyledTextLabel issue with URL's - iphone

I have added a TTStyledTextLabel inside my CustomCell for my UITableView, the code is:
TTStyledTextLabel * tt_title = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(50, cell.frame.origin.y, 640, 200)] autorelease];
tt_title.text = [TTStyledText textWithURLs:[[self.posts objectAtIndex:indexPath.row] message]];
tt_title.font = [UIFont systemFontOfSize:15];
tt_title.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
[tt_title sizeToFit];
[cell addSubview:tt_title
];
The NSString passed in has a URL, however when I click on the URL it doesn't do anything.. not even opening it up in safari.. why is this?

Not sure exactly without seeing what is in the actual string you're passing in to TTStyledText, but as an alternative you could try using
[TTStyledText textFromXHTML:[[self.posts objectAtIndex:indexPath.row] message] lineBreaks:NO URLs:YES];
and add <a href=''></a> to your string

Related

How to make the text in UILabel fill all space in UILabel?

UILabel *label = [[UILabel alloc] init];
label.text = #"Hello";
[label sizeToFit];
CGSize size = label.frame.size;
Usually we get the size of the label in this way, but text in the UILabel does not fill all space in it, there's always some margin around the text. That makes drawing label difficult to be exactly the same as visual draft. Anyone could help ?
for example: If you want to put an icon at the bottom of some text, let the margin between them be, for example 50px, and you write icon.frame = CGRectMake(10,textLabel.frame.size.height + textLabel.frame.origin.y + 50, 100, 100); but since the text can't fill all space in UILabel, so the margin in this way should be a bit larger than it should be. So I want to figure out a better way, thanks.
try with this bellow method which returns dynamic height of UILable with its text content... we this method you can set frame of UILable with its text content...
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use it like bellow...
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:CGRectMake(110, 31, 200, 50)];
lblAddress.text = #"your Text ";
lblAddress.lineBreakMode = UILineBreakModeWordWrap;
lblAddress.numberOfLines = 0;
lblAddress.font = [UIFont fontWithName:#"Helvetica" size:12];
lblAddress.frame = CGRectMake(lblAddress.frame.origin.x, lblAddress.frame.origin.y,
200,[self calculateHeightOfTextFromWidth:lblAddress.text :lblAddress.font :200 :UILineBreakModeWordWrap] );
lblAddress.textColor = [UIColor darkGrayColor];
[self.view addSubview:lblAddress];
See My Blog..
i hope you get some idea from this post...

Print Asterisk symbol in iPhone label

I am trying to print ascii values in label on one of the iPhone Application i am working on.I want the output to be something like this.How do i do it?
myLabel.text = #"Cutomer's First Name*";
Edited for making different color text you can use TTTAttributedLabel.
Below is the code
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
NSString* text = #"Cutomer's First Name*";
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString){
//NSMutableAttributedString *mutableAttributedString;
NSRange whiteRange = [text rangeOfString:#"*"];
if (whiteRange.location != NSNotFound) {
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:whiteRange];
}
return mutableAttributedString;
}];
label.font = [UIFont fontWithName: #"Helvetica Neue" size: 10];

iPhone/Objective - C - How to display the whole text of a long string in a UILabel

I have a very long string which is something like that below...
[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*3],[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4,rootof([[-3439170900,-50638826700,864180632700,9670592794500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*2+rootof([[1477010700,22974524100,-369910322100,-4442729593500],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)*4]],[sqrt(sqrt(221)+15),sqrt(-sqrt(221)+15)]
How can i display the whole thing in a UILabel with more than one lines (0)?
Also, although there are no linebreaks ("\n") in the string, the result is displayed like below....
[[sqrt(221)-10,-sqrt(221)- 10],[11,11]],[[rootof([[474368400,2 37184200,- 125944810200,258530778000],[1, 0,- 472,884,42215]])/(sqrt(221)*78270
Any help?
PS I could upload an image but i am not allowed beacause i am a new user...
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
set the UILabel as a multiline
MyLbl.numberOfLines = xxx;
if you have not enough space for set the UIlabel then also set the textView As an not editable property:
Mytxt = [[UITextView alloc]initWithFrame:CGRectMake(xx.0, x.0, xx.0,xx.0)];
Mytxt.transform = transform;
Mytxt.adjustsFontSizeToFitWidth = YES;
Mytxt.textColor = [UIColor blackColor];
Mytxt.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Mytxt setFont:[UIFont fontWithName:#"Arial" size:xx.0]];
Mytxt.backgroundColor = [UIColor clearColor];
Mytxt.delegate = self;
Mytxt.userInteractionEnabled=YES;
Mytxt.editable=NO;
i hope you get the answer.
You can use UITextView with auto size with non editable mode instead of UILabel (or)
CGSize labelSize = yourString sizeWithFont:cellFont
constrainedToSize:CGSizeMake(190.0f, MAXFLOAT); lineBreakMode:UILineBreakModeWordWrap];
This labelsize will dynamically define the label height you can assign to your label
try this code
NSString *yourString = #"[[sqrt(221)-10,-sqrt(221)-10],[11,11]],[[rootof([[474368400,237184200,-125944810200,258530778000],[1,0,-472,884,42215]])/(sqrt(221)*782707860000)+rootof([[409681800,9099248400,-99876110400,-1875803589000],[1,0,-472,884,42215]])";
UILabel *lblNameDesc=[[UILabel alloc] initWithFrame:CGRectMake(55, 10, 250, 21)];
[lblNameDesc setNumberOfLines:0];
lblNameDesc.text=yourString;
[lblNameDesc setBackgroundColor:[UIColor clearColor]];
[lblNameDesc setTextColor:[UIColor colorWithRed:64.0/255.0 green:64.0/255.0 blue:64.0/255.0 alpha:1.0f]];
[self.view addSubview:lblNameDesc];
// [lblNameDesc release];
[lblNameDesc sizeToFit];
supoose your string is. make your fond and width what ever you want:-
NSString longString = #"dsjkghsdkjnfgvsdjgsl,v.........................";
CGSize maximumLabelSize = CGSizeMake(200,MAXFLOAT);
CGSize expectedLabelSize = [longString sizeWithFont:[UIFont fontWithName:#"Aial-BoldMT" size:15.0] constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, expectedLabelSize.height)];
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.numberOfLines = 0;
myLabel.text = longString ;
may this will help you
Try This...
textLabel.lineBreakMode = YES;
textLabel.numberOfLines = 10; // or many that you need...
You want UILineBreakModeCharacterWrap if you want your text to be evenly distributed along several lines (so it doesn't try to break at work boundaries).

Having a url appear in a UITextview

This code works well to make a url appear in a UITextview
UITextView * descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];
descriptionTextView.textColor = [UIColor whiteColor];
descriptionTextView.dataDetectorTypes = UIDataDetectorTypeLink;
descriptionTextView.font = [UIFont fontWithName:#"Helvetica" size:16];
descriptionTextView.backgroundColor = [UIColor clearColor];
descriptionTextView.text = #"Click to go to the google website, http://www.google.com ";
descriptionTextView.autocorrectionType = UITextAutocorrectionTypeNo;
descriptionTextView.keyboardType = UIKeyboardTypeDefault;
descriptionTextView.returnKeyType = UIReturnKeyDone;
descriptionTextView.editable = NO;
descriptionTextView.tag = 1;
[self.view addSubview:descriptionTextView];
The problem is that I the whole url I write appears, http://www.google.com
Is there a way I can use just a single word to contain the link? So the user can only see 'Goggle' written in blue and when they click that work it opens safari.
Many Thanks,
-Code
you can do it use a UIWebview
use
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
and code the whole message then wrap the link to
[webView.loadHTMLString:#"Google" baseURL:nil];
I know this is super old thread. UITextView supports rich text/HTML. You should create NSAttributedString and assign it to attributedText property to manipulate with non plain text content. Like so:
NSString *htmlString = #"<your HTML code>";
textView.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];

UItableviewcell with muliple labels and custom height

I have added multiple labels in My Tableview cell. I am displaying facebook message and sender name and photos.For some posts it might be possible that message is unavailable or photo is unavailable .. I checking null condition and if there is no value present I am not creating label for that particular cell... now some label has large string , some may have small.. I am confused with cell's height ,, I am able to get dynamic height for 1 label like this in example dynamic height but how can I manage Height according to number of label's text... my code is like
if ([(Facebook * )[tableArray objectAtIndex:indexPath.row]sender]!= nil) {
labelSender = [[UILabel alloc]initWithFrame:CGRectMake(image_view.frame.size.width+20, 20, 120, 20) ];
labelSender.text = [NSString stringWithFormat:#"%#",[(Facebook * )[tableArray objectAtIndex:indexPath.row]sender]];
[labelSender setLineBreakMode:UILineBreakModeWordWrap];
[labelSender setNumberOfLines:0];
labelSender.textColor = [UIColor colorWithRed:59/255.0 green:89/255.0 blue:153/255.0 alpha:1.0];
labelSender.font = [UIFont fontWithName:#"Arial" size:15.0];
[cell.contentView addSubview:labelSender];
[labelSender release];
}
if ([(Facebook * )[tableArray objectAtIndex:indexPath.row]post]!= nil) {
NSLog(#"post is ==%#",[(Facebook * )[tableArray objectAtIndex:indexPath.row]post]);
labelMessage = [[UILabel alloc]initWithFrame:CGRectMake(image_view.frame.size.width+20, labelSender.frame.size.height + 20, 200, 20)];
labelMessage.text = [NSString stringWithFormat:#"%#",[(Facebook * )[tableArray objectAtIndex:indexPath.row]post]];
[labelMessage setLineBreakMode:UILineBreakModeWordWrap];
[labelMessage setNumberOfLines:0];
labelMessage.backgroundColor = [UIColor yellowColor];
labelMessage.font = [UIFont fontWithName:#"Arial" size:13.0];
[cell.contentView addSubview:labelMessage];
[labelMessage release];
}
please help
I feel its better to go for UITextView whenever handling dynamic content.
You should store the displayed labels in an NSMutableArray that is a property of the cell. That way you can iterate through simply by using:
for (UILabel *tmpLabel in yourLabelArray){
// do: add height to sum
}
This will save you an the if (labelExists) for every label.
Good luck!