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.
Related
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];
I have the following code which adds a label into a footer of a UITableView, so that I can format the text (white, etc.)
It works ok, but it gives me a leak warning for the "headerLabel" when analyzing it on the line with the "return"
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;
if (section==0) {
headerLabel.text = #"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element
}
[customView addSubview:headerLabel];
//[headerLabel release];
return customView;
// [customView release];
I've tried to put the release here and there, but it's always the same.
I'd appreciate some feedback from you guys.
try
[headerLabel release];
return [customView autorelease];
You have to release headerLabel before exiting the method:
[headerView release];
You probably should autorelease customView unless your method name includes the words new, alloc or copy (in that case, the caller would have to release the returned view):
return [customView autorelease];
autorelease your customView and make sure you are releasing headerLabel after you add it as a subview. Anytime you call alloc/init you are taking ownership, you need to make sure you release those objects. Since you are returning customView from this method it makes sense to defer your release of that object (using autorelease) so it can be used by the calling object.
// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc]
initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)]
autorelease];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;
if (section==0) {
headerLabel.text = #"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element
}
[customView addSubview:headerLabel];
[headerLabel release];
return customView;
Based on the code sample you've got here, the first release would be correct. (Releasing after the return statement wouldn't make sense). You took ownership of the object when you created it, and you need to release it.
You can use Instruments to track where object is being retained and released; you can see a history of the leaky object to see exactly what's going on. That would be your best bet here.
Launch your app with the Leaks instrument, and when you find the leaking object, click the arrow to the right of the address. This will show you the object's history - every retain and release.
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
OK I am trying to retrieve a variable from this text field from this example http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html. He says to do this :
"To get the text entered you just need to set a delegate for the text field and the alert, as shown in the example code above. Then you can use textFieldDidEndEditing: to get the value and store it somewhere temporary. When alertView:didDismissWithButtonIndex: is called, you can look up the saved value, and either use it or discard it depending on what button was pressed."
Thing is I'm so new to iOS and objective c that this means nothing to me. To me the text field delegate is set to self-- passwordField.delegate = self; Does anyone have an example to show? So I can see how to retrieve the entered text.
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Phone Number" message:#"\n\n\n"
delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",nil) otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text =#"Cell Phone Number xxx-xxx-xxxx";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,9)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
Create the textFieldDidEndEditing: delegate method in the same class:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *textValue = textField.text;
NSLog(#"Value: %#", textValue);
}
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.