How to make UIImage on setImage UIButton more higher - ios

What I want is make UIButton adhere to the UIView menu when UIButton isSelected.
The black square on UINavigation is UIImage after UIButton selected.
My question is how make UIImage (black square) more higher than UIButton after selected?
this my code on UIButton Class .m:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//...Our red stretchy
UIImage *redImage = [UIImage imageNamed:#"button_red.png"];
UIImage *redStretchy = [redImage stretchableImageWithLeftCapWidth:redImage.size.width / 2 topCapHeight:redImage.size.height / 2];
//...Initialization code
[self setFrame:frame];
[self setTitle:TITLE forState:UIControlStateNormal];
[self setBackgroundImage:redStretchy forState:UIControlStateNormal];
[[self titleLabel] setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]];
[[self titleLabel] setShadowOffset:CGSizeMake(FONT_SHADOW_OFFSET, FONT_SHADOW_OFFSET)];
//...add target
[self addTarget:self action:#selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
this the target methode:
//...add target
- (void)actionButton:(id)sender {
if ([sender isSelected]) {
} else {
CGSize buttonSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
[sender setImage:[self imageButton:buttonSize] forState:UIControlStateSelected];
}
if (delegate != nil && [delegate conformsToProtocol:#protocol(ButtonProtocol)]) {
if ([delegate respondsToSelector:#selector(actionButton:)]) {
[delegate performSelector:#selector(actionButton:) withObject:sender];
}
}
}
and this methode create UIIMage:
//...create image button
- (UIImage *)imageButton:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef currentContex = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGRect firstRect = CGRectMake(0.0f, 0.0f, 60.f, 30.0f);
CGPathAddRect(path, NULL, firstRect);
CGContextAddPath(currentContex, path);
UIColor *fillColor = [UIColor blackColor];
CGContextSetFillColorWithColor(currentContex, fillColor.CGColor);
CGContextDrawPath(currentContex, kCGPathFill);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Related

Image crop stretching image from camera

I'm using this project: http://code4app.net/ios/Perfect-Image-Cropper/51de11576803fa4548000004 for cropping images.
The fact is that is working perfect with photos imported from Library, but not with image from camera.
When selecting image from camera it's showing cropped but stretched.
#interface ImageViewController (){
ImageCropperView *cropper;
UIView *containerView;
UIImageView *finImageView;
UIButton *removeBtn;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// Media is an image
UIImage *image = info[UIImagePickerControllerOriginalImage];
if (image != nil) {
[self resetCropper];
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubview:containerView];
cropper = [[ImageCropperView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)]
[cropper setup];
cropper.image = image;
// Adding the crop button
UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cropBtn addTarget:self action:#selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
[cropBtn setTitle:#"Select" forState:UIControlStateNormal];
cropBtn.frame = CGRectMake(140, 160, 80, 30.0);
[containerView addSubview:cropBtn];
[containerView addSubview:cropper];
}
}
}
// Action when crop button clicked
- (IBAction)cropImage:(id)sender
{
[cropper finishCropping];
UIImage *newImage = cropper.croppedImage;
finImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
finImageView.image = newImage;
[cropper removeFromSuperview];
[containerView removeFromSuperview];
[self.scrollView insertSubview:finImageView belowSubview:self.actionsTab];
// Adding a button for remove
removeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
removeBtn.frame = CGRectMake( 0, 0, 22, 22);
[removeBtn setBackgroundImage:[UIImage imageNamed:#"remove"] forState:UIControlStateNormal];
[removeBtn addTarget:self action:#selector(removePhoto:) forControlEvents:UIControlEventTouchUpInside];
[finImageView insertSubview:removeBtn aboveSubview:finImageView];
finImageView.userInteractionEnabled = YES;
}
- (IBAction)removePhoto:(id)sender{
[self resetCropper];
[finImageView removeFromSuperview];
}
- (void)resetCropper
{
[cropper reset];
finImageView.image = nil;
[removeBtn removeFromSuperview];
}
Any help will be very appreciate!

UIButton image does not show in Navigation Bar

I am trying to a slide menu. The only problem is that the image for the UIButton does not show at runtime. This is the code I have written.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath
{
NSString *identifier;
switch (indexPath.row) {
case 0:
identifier=#"firstSegue";
break;
case 1:
identifier=#"secondSegue";
break;
}
return identifier;
}
-(void)configureLeftMenuButton:(UIButton *)button
{
CGRect frame = button.frame;
frame.origin=(CGPoint){0.0};
frame.size = (CGSize){40.40};
button.frame = frame;
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
}
I suppose you are using navigation controller. If so, implementation could look like:
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureLeftMenuButton];
}
-(void)configureLeftMenuButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"icon-menu"] forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
[button addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlStateNormal];
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = menuItem;
}

how to create BOOL property for UIButton for checked selecting

I create many button with loop from array but I want when click on any button I could check select state this button with one bool property with name : Selected.
when any button that I click on it selected change background to red color and when deselected change background to blue color.
please guide me how to create one property for any button.
this is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSMutableArray *arrayString = [[NSMutableArray alloc]initWithObjects:#"Mohammad",#"Hamidreza",#"Ahmad",#"Amirhossein",#"javad",#"asdasddasdsdasd", nil];
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:20];
CGFloat x = 5;
CGFloat y = 80;
for (NSString *text in arrayString) {
UIButton *button = [[UIButton alloc]init];
CGFloat width = [self widthOfString:text withFont:myFont];
CGFloat height = [self heightOfString:text withFont:myFont];
if ((x + width) > 320) {
NSLog(#"after line");
x = 5;
y += (height+20)+10;
button.frame = CGRectMake(x, y, width+20, height+20);
[button setTitle:text forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
x += (width+20)+10;
}
else {
NSLog(#"this line");
button.frame = CGRectMake(x, y, width+20, height+20);
[button setTitle:text forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
x += (width+20)+10;
}
[button addTarget:self action:#selector(Selected:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(IBAction)Selected:(id)sender{
if (//check sender button property bool selected) {
}
else {
}
}
Try this code:-
- (void)viewDidLoad
{
[super viewDidLoad];
[self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateHighlighted)];
[self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateSelected)];
[self.button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:(UIControlStateNormal)];
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Please, name your method starting with a lowercase. That's not related to your issue, but that's convention.
Since your question is more about "How do I get the button that was touched ?", than actually do your thing:
-(IBAction)selected:(id)sender //may rename this method to, it's quite strange, look like a "state/getter"
{
UIButton *button = (UIButton *)sender;
//Do something
if ([[button backgroundColor] isEqual:[selectedColor]])
{
//Do something
}
else
{
//Do something
}
}
my dude try this :
you don't need to create property for UIButton for this. you can do it with this code:
-(IBAction)Selected:(id)sender{
UIButton *button = (UIButton *)sender;
if (!button.selected) {
button.backgroundColor = [UIColor blueColor];
button.selected = YES;
}
else{
button.backgroundColor = [UIColor redColor];
button.selected = NO;
}
}
this code working for any button that you created!!! :D

iOS UIButton with transparent title on white background

I have a custom UIButton with transparent background and white title.
I'm looking for a simple solution to invert it on highlight (white background and transparent title) as it is achieved on system UISegmentedControl.
Is there simpler solution than inverting alpha on snapshot used as a CALayer mask?
If not, could you tell how can I invert CALayer alpha?
You can draw the text on a CGContext using Destination Out as blend mode.
This code seems to work:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UIFont* font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
NSString* buttonText = #"BUTTON";
CGSize buttonSize = CGSizeMake(200, 50);
NSDictionary* textAttributes = #{NSFontAttributeName:font};
CGSize textSize = [buttonText sizeWithAttributes:textAttributes];
UIGraphicsBeginImageContextWithOptions(buttonSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, buttonSize.width, buttonSize.height)];
CGContextAddPath(ctx, path.CGPath);
CGContextFillPath(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
CGPoint center = CGPointMake(buttonSize.width/2-textSize.width/2, buttonSize.height/2-textSize.height/2);
[#"BUTTON" drawAtPoint:center withAttributes:textAttributes];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* imgView = [[UIImageView alloc] initWithImage:viewImage];
[self.view addSubview:imgView];
}
By the way, if you want to set the UIImage in a UIButton instead of just adding it to a view:
UIButton* boton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, viewImage.size.width, viewImage.size.height)];
[boton setBackgroundColor:[UIColor clearColor]];
[boton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
boton.titleLabel.font = font;
[boton setTitle:buttonText forState:UIControlStateNormal];
[boton setImage:viewImage forState:UIControlStateHighlighted];
[self.view addSubview:boton];
You could set
-(void)viewDidload: {
[yourButton setTitleColor:(UIColor *)color
forState:UIControlStateHighlighted];
[yourButton addTarget:self
action:#selector(changeButtonBackGroundColor:)
forControlEvents:UIControlEventTouchDown];
}
-(void)changeButtonBackGroundColor:(UIButton *)button {
[button setBackgroundColor:[UIColor yourColor]];
}
remember to change it back with events: UIControlEventTouchUpInside and UIControlEventTouchOutSide :)

Dealing with a position for a single UIButton in a custom UIAlertView

I've a nice function for customizing UIAlertView, it works perfect with 2 buttons but the issue is, how do I know there is only 1 button and not 2 buttons?
Or how do I solve the problematic positioning with only 1 button?
My code is:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]])
{
UIImageView *imageView = (UIImageView*)obj;
float width = 284.0;
float height = 141.0;
CGSize size = CGSizeZero;
size.width = width;
size.height = height;
UIImage *newImage = [self imageWithImage:[UIImage imageNamed:#"CustomAlertView"] scaledToSize:size];
imageView.image = newImage;
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor clearColor];
label.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_on"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_on"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
OK, just figure it out by myself, I added:
// Determine how many buttons we deal with
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
for (id obj in self.subviews)
{
if ([obj isKindOfClass:[UIButton class]])
{
self.numOfButtons++;
}
}
NSLog(#"self.numOfButtons: %d", self.numOfButtons);
});
Right after:
- (void)layoutSubviews
Then I know how many buttons I deal with.

Resources