I'm creating a UILabel this way and it works just fine the first time..
CGRect labelFrame = CGRectMake(98, 286, 600, 180);
UILabel *lblQuestionTitle = [[UILabel alloc] initWithFrame:labelFrame];
[lblQuestionTitle setBackgroundColor:[UIColor clearColor]];
[lblQuestionTitle setFont: [UIFont fontWithName:#"Helvetica" size:17.0]];
[lblQuestionTitle setText:labelText];
[lblQuestionTitle setNumberOfLines:0];
[lblQuestionTitle sizeToFit];
[self.view addSubview:lblQuestionTitle];
The problem is when I use this code later, the new text appears over the old text.. How can I fix this?
Instead of reusing the code, simply overwrite the Label text.
[lblQuestionTitle setText:#"New label text!"];
If you really want an entire new button, simply set lblQuestionTitle to hidden before adding the new label
[lblQuestionTitle hidden:YES];
Related
I want to use QListWidget to display big images, e.g. 3508×4961 size in pixels, each list item will display one image. The image is set in a Qlabel and the Qlabel is set into the QListWidgetItem by setItemWidget(). When my program is running, the list item cannot display the entire image because the image is too large. It only displays the upper part of an image. When I scroll down, the list item changes to the next image immediately instead of showing the lower part of the current image gradually. Does anyone know how to show the lower part of each image by scrolling?
Here is my code,
QImage *image = new QImage("/home/sk/image1.png");
QLabel *label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
QListWidgetItem *ite = new QListWidgetItem;
auto size = label->sizeHint();
ite->setSizeHint(label->sizeHint());
size = ite->sizeHint();
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
image = new QImage("/home/sk/image2.png");
label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
ite = new QListWidgetItem;
ite->setSizeHint(label->sizeHint());
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
image = new QImage("/home/sk/image3.png");
label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
ite = new QListWidgetItem;
ite->setSizeHint(label->sizeHint());
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
have you tried changing the scroll mode to ScrollPerPixel
setVerticalScrollMode
and
setHorizontalScrollMode
ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
and in order to change the scroll step
ui->listWidget->verticalScrollBar()->setSingleStep(10);
ui->listWidget->verticalScrollBar()->setPageStep(20);
these values will be used for scrolling
I'm adding a QListWidgetItems to a QListWidget. Is there any way to set the size of the QListWidgetItem according to it's content data?
QSize size(50, 20);
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
newItem1->setSizeHint(size);
listWidget->addItem(newItem1); //listWidget is previously created
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
newItem2->setSizeHint(size);
listWidget->addItem(newItem2);
Text of newItem1 is displayed without any problem. But the newItem2 text is not fully displayed. It only shows few characters and then "..." as text elide. How to show the complete text without the elide? I want to set the size according to the size of item's data without setting any constant numbers.
I simply copy/pasted your code, removed setSizeHint() function call for both items and added some of mine. Here's working code:
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
ui->listWidget->addItem(newItem1);
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
ui->listWidget->addItem(newItem2);
ui->listWidget->setFixedSize(ui->listWidget->sizeHintForColumn(0) + ui->listWidget->frameWidth() * 2,
ui->listWidget->sizeHintForRow(0) * ui->listWidget->count() + 2 * ui->listWidget->frameWidth());
As you can see, both items are fully displayed.
Actually I have accidentally enabled uniformItemSizes for the QListWidget. When I disable that, QListWidgetItems were automatically resized according to it's content.
Issue :
I have use below code to change Title and set underline of UIButton at run time.
[btnTemp setTitle:#"Test" forState:UIControlStateNormal];
I have got below issues:
Underline is not showing
Title not change
Title color is display default black.
To overcome this issue I have implement below code:
NSDictionary *attribs = #{NSForegroundColorAttributeName: _btnTemp.titleLabel.textColor,
NSFontAttributeName: _btnTemp.titleLabel.font,
NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)
};
NSAttributedString *strAttributed = [[NSAttributedString alloc] initWithString:#"Test" attributes:attribs];
[btnTemp setAttributedTitle:strAttributed forState:UIControlStateNormal];
I use the KLCPopup library to display a popup in my app.
I have to add 2 buttons horizontally on the bottom of this popup, like the standard iOS UIAlerView. I cannot align this two buttons horizontally.
Here is the result I want:
But here is the result I have:
Here is the code I use to create this popup:
UIView* contentView = [[UIView alloc] init];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel* dismissLabel = [[UILabel alloc] init];
dismissLabel.translatesAutoresizingMaskIntoConstraints = NO;
dismissLabel.numberOfLines = 0;
[dismissLabel setTextAlignment:NSTextAlignmentCenter];
dismissLabel.lineBreakMode = NSLineBreakByWordWrapping;
dismissLabel.preferredMaxLayoutWidth = 200;
UIButton* dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.translatesAutoresizingMaskIntoConstraints = NO;
dismissButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
[contentView addSubview:dismissLabel];
[contentView addSubview:dismissButton];
UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
cancelButton.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
[contentView addSubview:cancelButton];
NSDictionary* views = NSDictionaryOfVariableBindings(contentView, dismissButton, cancelButton, dismissLabel);
[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-(16)-[dismissLabel]-(16)-[dismissButton]-(16)-[cancelButton]-(16)-|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:views]];
[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-(18)-[dismissLabel]-(18)-|"
options:0
metrics:nil
views:views]];
// Show in popup
KLCPopupLayout layout = KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter,
KLCPopupVerticalLayoutCenter);
KLCPopup* popup = [KLCPopup popupWithContentView:contentView
showType:KLCPopupShowTypeFadeIn
dismissType:KLCPopupDismissTypeGrowOut
maskType:KLCPopupMaskTypeDimmed
dismissOnBackgroundTouch:NO
dismissOnContentTouch:NO];
[popup showWithLayout:layout];
I think the problem on my code is the constraint.I've tried many possibilities but I cannot find the right way to do this.
Any advice will be helpful!
Thanks
You've added all three views to the vertical constraint and that's why they are all one after another. Do something like this:
V:|-16-[dismissLabel]
V:|-80-[dismissButton]
V:|-80-[cancelButton]
H:|-18-[dismissLabel]-18-|
H:[dismissButton(50)]-100-[cancelButton(50)]
I haven't tested it but something like that could work. You might have to modify the last H value or maybe add a centering option to it. There are probably better ways where you could group some of those things but this should work too.
Can any body give me the solution for displaying UINavigationItem prompt text in 2 lines?
There is not a built-in way to do this. Below is a work-around that seems to work pretty well that I put together from stackOverflow post UINavigationItem with prompt and activity indicator
Here is a simulator screen shot of what it creates:
Note that since the text is a UILabel you can modify its color, font, or anything else too.
// I have this code in viewDidLoad
UIView *viewContainingPrompt;
UIBarButtonItem *promptButtonItem;
// Configuring the prompt title of the navigation bar so it is present but empty
[self.navigationItem setPrompt: #""];
// We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
// A subview of viewContainingPrompt will be a UILabel (headerLabel)
// We need to have this "intermediate" view to position the label at the right position
// (the UIBarButtonItem ignores the origin and height of its custom view)
viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Choose a width that puts 10 points on either end...
CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
// Note that the '-60' below is determined by the width of the back button
// If someone can figure out how to determine this width at runtime this code
// would be much more robust.
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerLabel.text = #"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
headerLabel.font = [UIFont systemFontOfSize: 14];
headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
headerLabel.shadowOffset = CGSizeMake( 0, 1 );
headerLabel.textAlignment = UITextAlignmentCenter;
[viewContainingPrompt addSubview: headerLabel];
//[headerLabel release]; // Uncomment if not using ARC
promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
self.navigationItem.leftBarButtonItem = promptButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
//[viewContainingPrompt release]; // Uncomment if not using ARC
//[promptButtonItem release]; // Uncomment if not using ARC
I would appreciate anyone's feedback on how to figure out the width of the back button during execution so that width did not have to be hard coded.
As it is I do not think there are any private APIs or other illegal code contained.