Label missing properties after creation via method - ios

Have been searching alot for this, but I think I'm doing something stupidly wrong. I have a lot of labels on my second screen, and they all have the same properties. That's why I create them via a method.
In ViewDidLoad I do this:
[self screenTwoLabelMaker:firstNameLabel withFrame:CGRectMake(30, 200, 200, 40) withText:#"First Name"];
That method is this one:
- (UILabel *)screenTwoLabelMaker:(UILabel *)sender withFrame:(CGRect)frame withText:(NSString *)text
{
sender = [[UILabel alloc] init];
sender.text = text;
sender.frame = frame;
sender.font = [UIFont systemFontOfSize:labelFontSize];
sender.textColor = [UIColor grayColor];
[self.scrollView addSubview:sender];
return sender;
}
Then I do this:
NSLog(#"firstNameLabel x: %f y:%f w:%f h:%f", firstNameLabel.frame.origin.x, firstNameLabel.frame.origin.y, firstNameLabel.frame.size.width, firstNameLabel.frame.size.height);
But the result is this:
MyApp[9608:60b] firstNameLabel x: 0.000000 y:0.000000 w:0.000000 h:0.000000
The weird thing is that the label is being put on the screen on the right place with the right text. So everything should be sunny and warm, no? Well, I also call a similar method for buttons, where there is a tag included. That tag is never active on the button (nil), so further programming for that button is killing me. For now I create them all by hand. Do you guys know what I'm doing wrong?

Your method returns your new label, so you need to assign it to the variable that you want to hold the reference. You don't need to pass in the variable to the method as the first thing you do is overwrite it. Use the following code -
firstNameLabel = [self screenTwoMakeLabelwithFrame:CGRectMake(30, 200, 200, 40) text:#"First Name"];
- (UILabel *)screenTwoMakeLabelwithFrame:(CGRect)frame text:(NSString *)text
{
UILabel newLabel = [[UILabel alloc] init];
newLabel.text = text;
newLabel.frame = frame;
newLabel.font = [UIFont systemFontOfSize:labelFontSize];
newLabel.textColor = [UIColor grayColor];
[self.scrollView addSubview:newLabel];
return newLabel;
}

Related

TTTAttributedLabel can detect links,but can't be pressed correctly

I am using TTTAttributedLabel to detect links, here is the code I init the label:
- (TTTAttributedLabel *)getLinkLabelWithSize:(CGSize)size text:(NSString *)text{
TTTAttributedLabel *linkLabel;
linkLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, size.width * 1.1, size.height)];
linkLabel.font = [UIFont systemFontOfSize:14.0f];
linkLabel.backgroundColor = [UIColor clearColor];
linkLabel.lineBreakMode = UILineBreakModeWordWrap;
linkLabel.numberOfLines = 0;
linkLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
linkLabel.delegate = self;
linkLabel.text = text;
return linkLabel;
}
I add the label to anther view(xxxView).
CGSize size = [str sizeWithFont:fon constrainedToSize:CGSizeMake(150, 40)];
[returnView addSubview:[self getLinkLabelWithSize:size text:str]];
And here is the label delegate method:
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];
}
The problem is that I have to press the xxxView many times to open the link. It's there something wrong with my codes?
Can anyone helps me , please.
check if there is TapGesture for your cell content view,if yes, remove Gesture from the text message cell
Just had this issue, got really desperate when realised there is a fix in new released version

Remove UINavigationBarButton SubView

I have the following method which adds a MKNumberBadgeView to the UINavigationBar of a UITableViewController -
-(void)counterBtn{
MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(25, -10, 40, 40)];
numberBadge.strokeColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:0];
numberBadge.fillColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:1];
numberBadge.shine = NO;
numberBadge.hideWhenZero = YES;
numberBadge.value = _countBtnNo;
NSLog(#"Value of count = %d", _countBtnNo);
[self.navigationController.navigationBar addSubview:numberBadge];
}
All works fine until I want to change the count value - I have the following function to change the value -
- (void)removeBtn{
NSLog(#"ddd");
_countBtnNo = 0;
[self counterBtn];
}
But this does nothing - So i'm guessing I need to remove the subview first before re-adding - though there doesnt seem to be a removeSubview:numberBadge method - so i'm struggling!
Store a reference to your numberBadge in a property:
#property(nonatomic, strong) MKNumberBadgeView *numberBadge;
Then initialize it. To remove it, just call
[_numberBadge removeFromSuperview];
Alternatively you can just update the value of the counter. No need to remove it.
_numberBadge.value = new value;

MarqueeLabel not working , its just showing as an UILabel on screen

I have included the files from the link https://github.com/cbpowell/MarqueeLabel in my project
MarqueeLabel *continuousLabel2 = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10, 440, self.view.frame.size.width-20, 20) rate:10 andFadeLength:10.0f];
continuousLabel2.tag = 101;
continuousLabel2.marqueeType = MLContinuous;
continuousLabel2.animationCurve = UIViewAnimationOptionCurveLinear;
continuousLabel2.continuousMarqueeExtraBuffer = 50.0f;
continuousLabel2.numberOfLines = 1;
continuousLabel2.opaque = YES;
continuousLabel2.enabled = YES;
continuousLabel2.textAlignment = NSTextAlignmentLeft;
continuousLabel2.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
continuousLabel2.backgroundColor = [UIColor clearColor];
continuousLabel2.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.000];
continuousLabel2.text = #"This is another long label that scrolls continuously with a custom space between labels! You can also tap it to pause and unpause it!"
[self.view addSubview :continuousLabel2];
I have pasted the above code in my project but the text just appears as an UILabel and not as marquee. Please advice
Put this at the end of your code
[continuousLabel2 restartLabel];
OR
[self performSelector:#selector(startlabel) withObject:nil afterDelay:1.0];
-(void)startlabel{
[continuousLabel2 restartLabel];
}
I use resetLabel to start the marquee animation.
Also don't forget to set the labelize property to NO. i.e.
[self.marqueeLabel resetLabel];
self.marqueeLabel.labelize= NO;
Courtesy of Github documentation of Marquee Label
"MarqueeLabel tries its best to automatically begin scrolling when appropriate, but sometimes the way your view/view controller appears onscreen can trip it up.
Using the restartLabel instance method to "manually" start scrolling on a MarqueeLabel"
[yourMarqueeLabel restartLabel];

Issue in deleting the textfield

I have two buttons for adding and deleting the textfields. Adding works fine but when I click on delete it deletes only the last added textfield. Here are my two methods:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
}
-(void)deleteTextField {
[keyTextField removeFromSuperview];
[valueTextField removeFromSuperview];
yAxisDistance = yAxisDistance-35;
}
I know it's an small issue but I am very new to this field so kindly help.
Use this code for remove specific textfield from UIView. But First you have to set tag of every UITextField in view when you create or add it in view.
for ( UITextField* textField in view.subviews )
{
if(textField.tag== 1)
{
[textField removeFromSuperview];
}
}
I think the problem lies with the outlet. Can you check the following:
Open the outlets that are connected to the textifeld in the IB. There should be gray dots inside circles on the left side of the editor. Are they seem correct for both text fields?
Setup a breakpoint inside deleteTextField method and check the two textfields. Verify that both properties are not nil.
PS: You don't need to add tags to your view, using properties is perfectly fine and even better in my opinion. The reason for your problem is something else. Also, you do not need to removeFromSuperview, you can also setHidden:YES.
Sorry that time I was not understand your problem...
I done with this:
Declare one
NSMutableArray *allTextfieldArray;
and initialise in
viewdidload
method..
now do:
-(void)addTextField {
keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, yAxisDistance, 150, 30)];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = #"Key Value";
keyTextField.delegate = self;
[self.view addSubview:keyTextField];
valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(165, yAxisDistance, 150, 30)];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = #"Value";
valueTextField.delegate = self;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+35;
[allTextfieldArray addObject:keyTextField];
[allTextfieldArray addObject:valueTextField];
}
if ([allTextfieldArray count]>0) {
UITextField *txtField = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField removeFromSuperview];
txtField = nil;
UITextField *txtField2 = [allTextfieldArray lastObject];
[allTextfieldArray removeLastObject];
[txtField2 removeFromSuperview];
txtField2 = nil;
yAxisDistance = yAxisDistance-35;
}
you store the last textField in your variables keyTextField and valueTextField. So when you call your deleteTextField the last both will be deleted. you must track which textFileds you want exactly to delete.
for example you could give all your textFields a tag number when you create them:
first create an int as counter:
#implementation YourClass {
int tagcounter;
}
in init method set your counter:
tagcounter = 0;
in your addTextField:
keyTextField.tag = tagcounter++;
valueTextField.tag = tagcounter++;
when the delete button is tapped, you must know the textfield tags and pass them to your deleteTextField method. There you could do something like:
-(void)deleteTextFieldWithTag:(int)tagnumber {
for (UIView *view in [self.view subviews])
{
if ([view isKindOfClass:[UITextField class]])
{
if (view.tag == tagnumber || view.tag == tagnumber+1) {
[view removeFromSuperview];
}
}
}

Shinobi Charts (iOS): subclassing SChartCrosshairMultiValueTooltip - how to use the 'labels' property?

So I'm subclassing SChartCrosshairMultiValueTooltip to implement a custom multi-value (OHLC) tooltip. I can get the OHLC data from the chart OK. But I'm having difficulty understanding how to present the data in the way the Shinobi frameworks intend me to.
Specifically, I do not understand how to use the labels property. I first assumed that it would be pre-populated with labels generated by the superclass calling keyValueDisplayPairsForDataPoint:onSeries:withXAxis:withYAxis:, but that is not correct as the debugger reveals that the labels array is empty.
So I tried calling that method and manually adding UILabels to the labels array in my overridden - (void)setDataPoint:(id<SChartData>)dataPoint fromSeries:(SChartSeries *)series fromChart:(ShinobiChart *)chart method, but nothing is shown. So then I tried adding them as a subviews to self.view. That works, but because I've just added them myself they are not styled according to the tooltip, and anyway that just feels wrong since there's virtually no point subclassing if I have to do everything myself. I also have to ensure [self layoutContents] is not called for this to work, which feels double-wrong.
The code that works as described above looks like this:-
- (void)setDataPoint:(id<SChartData>)dataPoint fromSeries:(SChartSeries *)series fromChart:(ShinobiChart *)chart
{
SChartMultiYDataPoint *dp = (SChartMultiYDataPoint *)dataPoint;
NSDictionary *dict = [self keyValueDisplayPairsForDataPoint:dp onSeries:series withXAxis:chart.xAxis withYAxis:chart.yAxis];
for (UILabel *label in self.labels)
{
[label removeFromSuperview];
}
[self.labels removeAllObjects];
CGFloat y = 0;
const CGFloat kHeight = 25.0f;
for (id key in [dict allKeys])
{
UILabel *keyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 80, kHeight)];
keyLabel.text = key;
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, y, 80, kHeight)];
valueLabel.text = dict[key];
[self.labels addObject:keyLabel];
[self.labels addObject:valueLabel];
y += kHeight;
[self addSubview:keyLabel];
[self addSubview:valueLabel];
}
}
- (void)setPosition:(struct SChartPoint)pos onCanvas:(SChartCanvas *)canvas
{
// [self layoutContents]; // do NOT call this
self.frame = CGRectMake(0, 0, 160, 100);
}
Does anyone know a better way?

Resources