UITextView throws EXC_BAD_ACCESS - ios

I've created a UITextView instance and I'm trying to set its font:
UITextView *dummy = [[UITextView alloc] initWithFrame:self.frame];
dummy.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:14];//self.font;
However, at that second line, I'm getting EXC_BAD_ACCESS error, hitting continue execution doesn't do anything. It still hangs at the same line forever, with no error message at the console. It also used to throw at self.font too (I'm using the code inside a working UILabel and its font is a valid font object). Why am I getting this error?
UPDATE: I've also tried setText: it also throws the same too. Am I missing something obvious?

Try this code.
UITextView *textview =[[UITextView alloc]init];
textview.frame=CGRectMake(200, 10, 500, 50);
//To make the border look very close to a UITextField
[textview.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textview.layer setBorderWidth:2.0];
textview.delegate=self;
//The rounded corner part, where you specify your view's corner radius:
textview.layer.cornerRadius = 5;
textview.clipsToBounds = YES;
[textview setFont:[UIFont fontWithName:#"Helvetica Neue" size:11]];
[textview setTextColor:[UIColor blueColor]];
[textview setReturnKeyType:UIReturnKeyDone];

I've found the solution.
I've tried [[UITextView alloc] initWithFrame:self.frame textContainer:nil]; instead of [[UITextView alloc] initWithFrame:self.frame]; and it worked. I don't understand why the other method didn't work, and this one works even though I'm using nil as the textContainer.

Related

SetLeftView In Label Objective-C

it has been asked before (using textfield, and I'm asking how to include a character not a small icon), and yes I have already tried using this SetLeftView to put a dollar sign '$' or whatever character I want beside the TEXTFIELD.
However, I do not want a textfield, but a label, and when I apply the same code to do what I want, it returns me an error, I mean Xcode fails to build the code.
Here is my code:
// We add a label to the top that will display the results
self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 80, TEXTAREA_WIDTH, TEXTAREA_HEIGHT)];
[resultLabel setBackgroundColor:[UIColor clearColor]];
[resultLabel setText:#"01234"];
[resultLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
resultLabel.textColor = RGB(255,255,255);
resultLabel.textAlignment = NSTextAlignmentCenter;
// Add it to the view
[self.view addSubview:resultLabel];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[resultLabel setLeftView:dollarSignLabel];
[resultLabel setLeftViewMode:UITextFieldViewModeAlways];
Error: No visible #interface for 'UILabel' declares the selector
'setLeftView'. Same error in the line of setLeftViewMode.
Again, this works if I use a textfield.
My working code (using textfield)
// adding a textField for input
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(viewHalf-30, 100, 200, 40)];
[myTextField setBackgroundColor:[UIColor clearColor]];
[myTextField setText:#"0"];
[myTextField setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
myTextField.textColor = RGB(255,255,255);
[[self view] addSubview:myTextField];
UILabel *dollarSignLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 40)];
dollarSignLabel.text = #"$";
dollarSignLabel.textColor = RGB(255,255,255);
[dollarSignLabel setFont:[UIFont fontWithName:#"AppleGothic" size:30.0f]];
[myTextField setLeftView:dollarSignLabel];
[myTextField setLeftViewMode:UITextFieldViewModeAlways];
The reason you can't apply that because UILabel doesn't have a method named setLeftView as UITextField do.
What you can do is :
Create two labels next to each other.
Set left labels trailingSpace to right label to 0. Arrange other constraints whatever you want.
Set left label's textAlingment property to NSTextAlignmentRight and right label's to NSTextAlignmentLeft.
Set dolar sign on a left label and numbers to another.
Since a label isn't editable by the user anyway, there is no reason not just to add your $ sign to the label itself.
label.text = [#"$" stringByAppedingString:yourText];
if the special symbol should be an image instead, then look at NSTextAttachment & draw attributed Text
hope this will help u out.
add a dollar image on your label.
override following method of UILabel
-(CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
bounds.origin.x =+leftMargin;
return bounds;
}
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect: CGRectInset(self.bounds, leftMargin , 0)];
}

UITextView responding to touches, but keyboard doesn't appear

I can't comprehend it. I have a uitextview which animates upward when it begins editing to make room for the keyboard. I tap the uitextview and registers that it has begun editing and it animates up but the keyboard doesn't appear. This has never happened before and I've tested the app hundreds of times. I even have the textview become first responder upon building the view and the keyboard still doesn't show.
I have the .m file and .h file as so that is not the issue..
Here is the set up code:
self.addDescriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)];
[self.addDescriptionTextView setBackgroundColor:[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:.5]];
[self.addDescriptionTextView setTextColor: [UIColor whiteColor]];
[self.addDescriptionTextView setFont:[UIFont fontWithName:#"Verdana" size:16]];
[self.addDescriptionTextView setDelegate:self];
self.addDescriptionTextView.textContainer.maximumNumberOfLines = 10;
[self.addDescriptionTextView.layoutManager textContainerChangedGeometry:self.addDescriptionTextView.textContainer];
[confirmView addSubview:self.addDescriptionTextView];
I ran your code on my iOS device and the keyboard came up,
Heres the viewDidLoad Code I ran,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.addDescriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 470, 320, 100)];
self.addDescriptionTextView.backgroundColor = [UIColor redColor];
[self.addDescriptionTextView setTextColor: [UIColor whiteColor]];
[self.addDescriptionTextView setFont:[UIFont fontWithName:#"Verdana" size:16]];
[self.addDescriptionTextView setDelegate:self];
self.addDescriptionTextView.textContainer.maximumNumberOfLines = 10;
[self.addDescriptionTextView.layoutManager textContainerChangedGeometry:self.addDescriptionTextView.textContainer];
self.addDescriptionTextView.editable = YES;
[self.view addSubview:self.addDescriptionTextView];
}
Try adding the UIView.editable line but it should be set to yes automatically.
It might have something to do with confirmView?
Also try changing the UIColor Background colour line I was getting errors with your one.
Edit: After running on iPhone4s your problem is the position you initiate the UIView (to see try changing the 470 to 0 in the CGRect to see the bug)

iOS Multiline Label NSLineBreakByTruncatingTail

I have a label with numberOfLines set to 2, lineBreakMode set to NSLineBreakByTruncatingTail. I was expecting that when the text extends beyond two lines, the label will truncate it with ellipses. But what is happening is that although the text is being truncated no ellipses are getting shown.
Why would this be? Thanks.
More details:
Following is the custom class for the UITableViewCell that contains the label (contentLabel) in question. The labels are updated in the loadConversation method.
#interface CPActivityStreamCell ()
#property(nonatomic, weak) CardDeck *cardDeck;
#property(nonatomic, weak) UIImageView *icon;
#end
#implementation CPActivityStreamCell
#synthesize contentLabel, eventLabel, avatarView;
- (void)awakeFromNib {
[super awakeFromNib];
self.backgroundColor = [UIColor clearColor];
// Initialize views
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
[eventLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[eventLabel setNumberOfLines:1];
[eventLabel setTextAlignment:NSTextAlignmentLeft];
[eventLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
eventLabel.backgroundColor = [UIColor clearColor];
eventLabel.font = [UIFont fontWithName:#"Antonio-Regular" size:12];
// Create the icon view
UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 15.0, 16.0, 16.0)];
[self addSubview:icon];
self.icon = icon;
CardDeck *deck = [[CardDeck alloc] initWithFrame:self.contentView.frame numberOfCards:1];
self.backgroundView = deck;
self.cardDeck = deck;
}
- (void)loadConversation:(CPConversation *)conversation {
[self loadAvatars:conversation.avatarUrls];
self.contentLabel.textColor = [UIColor blackColor];
self.eventLabel.text = [conversation.eventTitle uppercaseString];
self.eventLabel.textColor = [UIColor colorWithWhite:0.55 alpha:1.0];
self.contentLabel.text = conversation.content;
[self loadEventTypeIcon:conversation.eventType white:NO];
[self updateDeck:conversation withFrame:self.contentView.frame];
if ([conversation.typeString isEqualToString:#"image"]) {
[self loadConversationImage:conversation];
}
}
//other methods
#end
The same label in IB:
Output:
The top cell's contentLabel is set to Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.:
The problem lies somewhere in the architecture of your initialization procedure, or possibly in something you are doing elsewhere (in code you have not shown in your question). You are saying
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
But this code is not being applied to the label we see in the screen shot. That much is clear, because if it were being applied, the text would be a light gray color.
Thus we may conclude that this code is not running or, if it is running, contentLabel is not hooked up property (perhaps it is nil) and the label is unaffected by it.
Finally, I may point out that on my machine I simply ran your code as given, directly, on a UILabel (not in a table view or anything), and it worked:
[contentLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[contentLabel setNumberOfLines:2];
[contentLabel setTextAlignment:NSTextAlignmentLeft];
[contentLabel setTextColor:[UIColor colorWithWhite:0.55 alpha:1.0]];
contentLabel.backgroundColor = [UIColor clearColor];
contentLabel.text = #"Start of a really long text to test truncation and display in activity stream. It should be truncated with ellipses. End.";
Thus I suggest that either your code is not running or not being applied to the label, or that some other code is coming along and changing the line break mode for this label. It is always best to convince oneself that something works generally for an extremely simple case (like mine, in the preceding code) and then try to figure out why particular results seem to differ from that...

UITextView Not responding

I am using textview in my code using the below code
UITextView *ObservationTextView3=[[UITextView alloc]init];
ObservationTextView3.frame=CGRectMake(45, ObservationLabel3.frame.origin.y+ObservationLabel3.frame.size.height, 410, 70);
[ObservationTextView3 setKeyboardType:UIKeyboardTypeDefault];
ObservationTextView3.font=[UIFont fontWithName:#"Helvetica" size:14];
ObservationTextView3.delegate=self;
ObservationTextView3.tag=139;
ObservationTextView3.text = #"";
ObservationTextView3.layer.borderWidth=1.0f;
ObservationTextView3.layer.borderColor=[UIColor lightGrayColor].CGColor;
ObservationTextView3.layer.cornerRadius=4.0f;
[_BeverageRoutineScroll addSubview:ObservationTextView3];
but i am unable to make this textview respond i've tried many things but evrything goes invane.
please help me out.
Try this:
[self.view bringSubviewToFront:yourTextView];
[self.view sendSubViewToBack:yourScrollview];
OR
[self.yourScrollView setUserInteractionEnabled:YES];
[self.yourTextView setUserInteractionEnabled:YES];
OR
check your scrollView's Content Size and Text View Frame

UITableViewStyleGrouped default header view?

I'm trying to implement a control for deleting entire sections, and it would look best in my app if the delete button was in the header, as opposed to an overlay like a UIPopoverView.
In the process of writing this question, I found the answer. Easy enough, once there's a starting point.
I got the bulk of the code from this blog which has only two posts, both from 2010.
Then I went back to this site just for the font color, since it's more trouble to break apart.
Three minor problems, all with the label.
- Font is too narrow
- Text color is too dark
- Label origin is wrong
The default font is known, so that comes first.
label.font = [UIFont boldSystemFontOfSize:17.0];
Color is next, since that's easy. Used an image editor's Eyedropper tool for this.
label.textColor = [UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1];
// Is there a difference between alpha:1 and alpha:1.000?
Then the hard part. A close guess, and then some tweaking for a perfect match.
label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
And now we have a custom implementation that perfectly matches the current Grouped header.
Finished code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
tableView.sectionHeaderHeight = headerView.frame.size.height;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:17.0]];
[label setShadowColor:[UIColor whiteColor]];
[label setShadowOffset:CGSizeMake(0, 1)];
[label setText:[self tableView:tableView titleForHeaderInSection:section]];
[label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
[headerView addSubview:label];
return headerView;
}
Found this SO answer after finding the right font/color myself. Oh well.
Edit:
For a title label that allows an effectively unlimited amount of text:
// before label init
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
NSUInteger maxWidth = headerView.frame.size.width-108;
CGSize labelSize = [title sizeWithFont:[UIFont systemFontOfSize:17.0]
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
if (labelSize.width < maxWidth) labelSize.width = maxWidth;
// after setFont:
[label setNumberOfLines:0];

Resources