Having a url appear in a UITextview - iphone

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

Related

Font size issue in iOS

I am trying to set the font of the label as per follow but it gives me zero CGSize for that.
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica Cyrillic Bold_5" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle sizeWithFont:abbrFont];//_addbrTitle is a NSString
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Help me to solve this.
Thank you.
Try this,
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //E
You can use this line to set the size for label..
[label setFont:[UIFont fontWithName:#"Helvetica" size:20.0]];
UILabel *_addbrTitle = [[UILabel alloc] init];
[_addbrTitle setText:#"Hello"];
UIFont *abbrFont = [UIFont fontWithName:#"Helvetica" size:50]; //Helvetica Cyrillic Bold_5 added Custom Font
[_addbrTitle setFont:abbrFont];
CGSize abbrSizeOfString = [_addbrTitle.text sizeWithFont:abbrFont];//_addbrTitle is a Label
NSLog(#"%f %f",abbrSizeOfString.width,abbrSizeOfString.height); //Everytime Prints (0.000,0.000)
Font name you have entered in the code does not exists in system. So its printing (0,0) every time. Enter a font name which exists in system it will print the size.
change your font name to some other built in font name then try
like this
yourLabel.font=[UIFont fontWithName:#"Helvetica" size:16];
Your font name is not current follow this IOS supporting fonts list and Font name
http://iosfonts.com/
NSURL *fontURL = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"your_font_name" ofType:#"TTF"]];
for example:
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
NSURL *fontURL2 = [NSURL URLWithString:[[NSBundle mainBundle]pathForResource:#"segoe_semi_bold" ofType:#"TTF"]];
now make add url to array
NSArray *arrFont =[[NSArray alloc]initWithObjects:fontURL1,fontURL2, nil];
int result =CTFontManagerRegisterFontsForURLs((CFArrayRef)arrFont, kCTFontManagerScopeUser, nil);
if(result)
{
NSLog("Font Install successfully");
}
now you can get font

UITextView dataDetectorTypes can't detect http://t.co/ links?

theTweet = [[[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)] autorelease];
theTweet.text = [[tweets objectAtIndex:index] objectForKey:#"text"];
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[tweetView addSubview:theTweet];
[[tweets objectAtIndex:index] objectForKey:#"text"]; contains a link with http://t.co/###### but it doesn't seem like the UITextView is detecting http://t.co links. Do I need to use a UIWebView instead?
One thing I've noticed is that in order for UITextViews to recognize links, you need to set selectable to YES. Example:
self.bodyTextView = [[UITextView alloc]initWithFrame:myFrame];
[self.bodyTextView setEditable:NO];
//this is the key
[self.bodyTextView setSelectable:YES];
[self.bodyTextView setDataDetectorTypes:UIDataDetectorTypeLink];
[self.bodyTextView setAttributedText:myAttributedText];
did you set: theTweet.dataDetectorTypes = UIDataDetectorTypeLink; ?
Now that you added that, I tried this code:
UITextView *theTweet;
theTweet = [[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)];
theTweet.text = #"http://t.co/######";
theTweet.editable = NO;
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[myview addSubview:theTweet];
and it works fine with me.
The error must be somewhere else. (did you turn off editable too?)
You need to set editable property NO
theTweet = [[[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)] autorelease];
theTweet.editable = NO; //add this line
theTweet.text = [[tweets objectAtIndex:index] objectForKey:#"text"];
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[tweetView addSubview:theTweet];
Maybe good to make extension so we don't have to memorize it...
#implementation UITextView (Extension)
- (instancetype)dataDetector :(UIDataDetectorTypes)types {
self.dataDetectorTypes = types;
if (types != UIDataDetectorTypeNone) self.selectable = true;
return self;
}
#end

TTStyledTextLabel issue with URL's

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

Strange crash relating to UITextField.text

I've got a view with 2 UITextFields on it. I fill in both fields and press a button which calls this code below.
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
NSLog(#"un: %#", userNameStr);
NSLog(#"cb: %#", clubFanStr);
The NSLogs show exactly what was typed into the fields.
I then press another button, a 3rd UITextField appears I put text in that, press a button and it saves, same as above.
Later on in the app when clubFanStr is used in an NSLog it crashes the app. Sometimes it doesnt crash the app if whatever is contained in clubFanStr seems to be somewhat valid.
userNameStr never crashes the app at the same point.
So I though this was strange at changed the code to this
userNameStr =[NSString stringWithString:clubNameTextField.text];
clubFanStr = [NSString stringWithString:textFieldRounded.text];
NSLog(#"un: %#", userNameStr);
NSLog(#"cb: %#", clubFanStr);
Basically swapping which string holds data from which UITextField.
So now app crashes later on when i try NSLog(#"%#",userNameStr);
So it seems that only the UITextField clubNameTextField is malfunctioning.
Here is code of both of there declarations.
textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(5, 40, 310, 30)];
textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
textFieldRounded.textColor = [UIColor blackColor]; //text color
textFieldRounded.font = [UIFont systemFontOfSize:17.0]; //font size
textFieldRounded.placeholder = #"User Name?"; //place holder
textFieldRounded.backgroundColor = [UIColor whiteColor]; //background color
textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;
textFieldRounded.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
textFieldRounded.returnKeyType = UIReturnKeyDone; // type of the return key
textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;
textFieldRounded.delegate = self;
textFieldRounded.hidden = YES;
[self.view addSubview:textFieldRounded];
clubNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 120, 310, 30)];
clubNameTextField.borderStyle = UITextBorderStyleRoundedRect;
clubNameTextField.textColor = [UIColor blackColor]; //text color
clubNameTextField.font = [UIFont systemFontOfSize:17.0]; //font size
clubNameTextField.placeholder = #"Team you support?"; //place holder
clubNameTextField.backgroundColor = [UIColor whiteColor]; //background color
clubNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
clubNameTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
clubNameTextField.returnKeyType = UIReturnKeyDone; // type of the return key
clubNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
clubNameTextField.delegate = self;
clubNameTextField.hidden = YES;
//textFieldRounded.editing lets you know of the text field is being currently edited
[self.view addSubview:clubNameTextField];
Anybody able to clue me into what I've done wrong?
Many Thanks,
-Code
Instead of
userNameStr =[NSString stringWithString:textFieldRounded.text];
clubFanStr = [NSString stringWithString:clubNameTextField.text];
try
userNameStr = [[NSString alloc] initWithString:textFieldRounded.text];
clubFanStr = [[NSString alloc] initWithString:clubNameTextField.text];
Is clubFanStr used in another method? You may want to retain it then, and the userNameStr as well.
Later on in the app when clubFanStr is used in an NSLog it crashes the app
This almost certainly means that you have a reference counting error, and the string has been deallocated. How are userNameStr and clubFanStr declared? You are assigning autoreleased values to them; either they are retain/copy properties, and you should be using self.<propertyName> to assign to, or they aren't and you should be retaining as you assign.

Strike-through font in objective C

How can I use strike-through font in objective C??? More specifically in UITableViewCell
cell.textLabel.text = name;
cell.detailTextLabel.text = quantity ;
cell.XXX = ??
this is Marin, the author of the attributed strings chapter in "iOS6 by Tutorials".
Since iOS6 there is actually a native support for a bunch of different text attributes, including strike-trough.
Here's a short example, which you can use for your cell text label:
NSDictionary* attributes = #{
NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};
NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:#"My Text" attributes:attributes];
cell.textLabel.attributedText = attrText;
That's all. Good luck!
EDIT: This answer is out of date as of iOS 6. Please see the more recent answers below
There is not native support for strike-through or underline fonts. You have to draw the lines yourself over the views for the labels.
This is silly since the font inspector for IB has options to set strike-through and underline, but these are promptly ignored if you try to set them.
CGRect frame = sender.titleLabel.frame;
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame];
strikethrough.opaque = YES;
strikethrough.backgroundColor = [UIColor clearColor];
strikethrough.text = #"------------------------------------------------";
strikethrough.lineBreakMode = UILineBreakModeClip;
[sender addSubview:strikethrough];
here is how you strikethrough your label. But remember, it only works after ios 6.0
NSNumber *strikeSize = [NSNumber numberWithInt:2];
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize
forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:#"Striking through it" attributes:strikeThroughAttribute];
strikeThroughLabel.attributedText = strikeThroughText;
1-Get the size of text which needs to strikethrough
CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip];
2-Create an line and add it to the text
UIView *viewUnderline = [[UIView alloc] init];
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1);
viewUnderline.backgroundColor = [UIColor grayColor];
[cell addSubview:viewUnderline];
[viewUnderline release];
Strikethrough is possible by using NSStrikeThroughAttributes and attributedText of UILabel. Here is the solution in Swift
let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1]
let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes)
strikeThroughLabel.attributedText = strikeThroughString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:#"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:#2
range:NSMakeRange(0, [attributeString length])];
yourLabel.attributedText = attributeString;
Have you thought about finding your own strikethrough font and loading it yourself? It isn't that hard, just add the UIFont to your Info.plist file and put the name of the font in there. Then you can manually set the text to that new strikethrough font.
See this post on loading custom font.
Can I embed a custom font...
UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)];
strikeView.backgroundColor = [UIColor redColor];
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2);
[myLabel addSubview:strikeView];
This will strike through the whole cell. In case you need it to look like completed task:
CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height / 2,size.width - 30, 1)];
line.backgroundColor = [UIColor grayColor]; // set your preferred color
[cell addSubview:line];
You may indicate some other value in CGRectMake instead of 15 - it's offset by X. In this case you'd better then decrease width by doubled value (in my case it's 15*2 = 30) in order it look nice.